﻿using Dapper;
using Estsh.Core.Base;
using Estsh.Core.Dapper;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Model.Result;
using Estsh.Core.Repositories;
using System.Collections;
using System.Data;
using System.Text;
using System.Security.Cryptography;
using System.Text.Json;
using Estsh.Core.Models;
using Estsh.Core.Model.Extend.Models;
using Newtonsoft.Json.Linq;
using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Model.Extend;
using SqlSugar;
using Estsh.Core.Util;
using System.Collections.Generic;
using NPOI.SS.Formula.Functions;
using static NPOI.HSSF.Util.HSSFColor;
using System.Security.Policy;

namespace Estsh.Core.Wms.Repositories
{
    public class MoveRepository : BaseRepository<BaseEntity>, IMoveRepository
    {
        public MoveRepository(DapperDbContext _dapperDbContext) : base(_dapperDbContext)
        {

        }


        /// <summary>
        /// 获取周转箱绑定关系
        /// </summary>
        /// <param name="rackNo"></param>
        /// <returns></returns>
        public List<WmsRackPackage_Extend> GetRackPackageInfo(string rackNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;
                StringBuilder sql = new StringBuilder(1024);
                sql.Append("SELECT * FROM wms_rack_package (nolock) WHERE enabled='Y' AND rack_no=@rackNo  ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@rackNo", rackNo);

                //return dbConn.QueryFirstOrDefault<List<WmsRackPackage_Extend>>(sql.ToString(), parameters);

                return dbConn.Query<WmsRackPackage_Extend>(sql.ToString(), parameters).ToList();
            }
        }


        /// <summary>
        /// 判断是否绑定了此零件
        /// </summary>
        /// <param name="partNo"></param>
        /// <returns></returns>
        public WmsRackPackage_Extend GetPackagePaetNo(string rackNo, string partNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;
                StringBuilder sql = new StringBuilder(1024);
                sql.Append("SELECT * FROM wms_rack_package(nolock) WHERE enabled='Y' AND  rack_no=@rackNo and part_no=@partNo ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@rackNo", rackNo);
                parameters.Add("@partNo", partNo);

                return dbConn.QueryFirstOrDefault<WmsRackPackage_Extend>(sql.ToString(), parameters);
            }
        }

        /// <summary>
        /// 获取周转箱信息
        /// </summary>
        /// <param name="rackNo"></param>
        /// <returns></returns>
        public WmsRack_Extend GetRackInfo(string rackNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;
                StringBuilder sql = new StringBuilder(1024);
                sql.Append("SELECT * FROM wms_rack (nolock) WHERE enabled='Y' AND packageType=1  AND rack_no=@rackNo ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@rackNo", rackNo);

                return dbConn.QueryFirstOrDefault<WmsRack_Extend>(sql.ToString(), parameters);
            }
        }

        public WmsRack_Extend GetRackInfo2(string rackNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;
                StringBuilder sql = new StringBuilder(1024);
                sql.Append("SELECT * FROM wms_rack (nolock) WHERE enabled='Y'   AND rack_no=@rackNo ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@rackNo", rackNo);

                return dbConn.QueryFirstOrDefault<WmsRack_Extend>(sql.ToString(), parameters);
            }
        }


        //获取箱条码状态
        public SysStock_Extend CheckCartonNoStatus(string cartonNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("select a.*,b.enum_name as stock_status from sys_stock (nolock) a left join sys_enum b on enum_type='sys_stock_status' and enum_value=status where carton_no=@carton_no  AND a.enabled='Y' ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@carton_no", cartonNo);

                return dbConn.QueryFirstOrDefault<SysStock_Extend>(sql.ToString(), parameters);
            }
        }

        public List<SysStock_Extend> CheckCartonNolist(string cartonNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("select a.*,b.enum_name as stock_status from sys_stock (nolock) a left join sys_enum b on enum_type='sys_stock_status' and enum_value=status where carton_no in ('" + cartonNo + "')  AND a.enabled='Y' ");
                DynamicParameters parameters = new DynamicParameters();
                //parameters.Add("@carton_no", cartonNo);

                return dbConn.Query<SysStock_Extend>(sql.ToString(), parameters).ToList();
            }
        }


        /// <summary>
        /// 获取周转箱和箱条码信息
        /// </summary>
        /// <param name="cartonNo"></param>
        /// <returns></returns>
        public List<SysStock> GetcartNoInfos(string cartonNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("select a.*,b.enum_name as stock_status from sys_stock (nolock) a left join sys_enum b on enum_type='sys_stock_status' and enum_value=status where carton_no in (" + cartonNo + ")  AND a.enabled='Y' ");
                DynamicParameters parameters = new DynamicParameters();
                //parameters.Add("@carton_no", cartonNo);

                return dbConn.Query<SysStock>(sql.ToString(), parameters).ToList();
            }
        }


        //获取库位与零件对应关系
        public List<SysLocate> CheckLocateStatus(string locateName)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append(@"SELECT a.*,B.*,c.erp_warehouse FROM dbo.sys_locate a (nolock) 
                            LEFT JOIN sys_locate_part b (nolock) ON a.locate_id = b.locate_id 
                            LEFT join sys_zone as c (nolock) on a.zone_id=c.zone_id  
                            WHERE a.locate_name =@locateName ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@locateName", locateName);

                return dbConn.Query<SysLocate>(sql.ToString(), parameters).ToList();
            }
        }

        //获取库位 已使用容量
        public List<SysStock> CheckUseStockCapacity(string locateName)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append(" SELECT * FROM dbo.sys_stock (nolock) WHERE locate_name = @locateName AND status IN ('50','60','80')  ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@locateName", locateName);
                return dbConn.Query<SysStock>(sql.ToString(), parameters).ToList();
            }
        }

        //获取箱条码状态 通过分组号
        public List<SysStock_Extend> CheckCartonNoStatusByPack(string cartonNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("select * from sys_stock (nolock) where group_no= (select group_no from sys_stock (nolock) where carton_no = @carton_no ) and status='50'  ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@carton_no", cartonNo);

                return dbConn.Query<SysStock_Extend>(sql.ToString(), parameters).ToList();
            }
        }




        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="rackPackageInfo"></param>
        /// <param name="loginId"></param>
        /// <returns></returns>
        public string InsertRackPackageInfo(SysStock cartStockInfo, SysStock rackStockInfo, string loginId, string rackNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                //for (int i = 0; i < rackPackageInfo.Count; i++)
                //{
                //新增料架与包装信息
                DynamicParameters updateStringparameters2 = new DynamicParameters();
                StringBuilder updateString1 = new StringBuilder(1024);
                StringBuilder insterString2 = new StringBuilder(1024);
                updateString1.Append(@"UPDATE [dbo].[sys_stock] SET locate_id=@locate_id,locate_name=@locate_name,warehouse_id=@warehouse_id,warehouse_name=@warehouse_name,update_time=@update_time,update_userid=@update_userid where ruid=@ruid");
                updateStringparameters2.Add("@locate_id", rackStockInfo.LocateId);
                updateStringparameters2.Add("@locate_name", rackStockInfo.LocateName);
                updateStringparameters2.Add("@warehouse_id", rackStockInfo.WarehouseId);
                updateStringparameters2.Add("@warehouse_name", rackStockInfo.WarehouseName);
                updateStringparameters2.Add("@ruid", cartStockInfo.Ruid);
                updateStringparameters2.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters2.Add("@update_userid", loginId);
                sqlStrings.Add(updateString1.ToString());
                parameterList.Add(updateStringparameters2);


                insterString2.Append(@"INSERT INTO [dbo].[wms_rack_package]
                                         ([rack_no]
                                         ,[corton_no]
                                         ,[packageType]
                                         ,[part_no]
                                         ,[factory_id]
                                         ,[factory_code]
                                         ,[enabled]
                                         ,[create_userid]
                                         ,[create_time]
                                         ,[update_userid]
                                         ,[update_time]
                                         ,[guid],[rack_num])
                                   VALUES (@rack_no,@corton_no,@pack_type,@part_no,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid,@rack_num)");
                updateStringparameters2 = new DynamicParameters();
                updateStringparameters2.Add("@rack_no", rackNo);//新台车号
                updateStringparameters2.Add("@corton_no", cartStockInfo.CartonNo);
                updateStringparameters2.Add("@pack_type", 1);
                updateStringparameters2.Add("@part_no", cartStockInfo.PartNo);
                updateStringparameters2.Add("@factory_id", cartStockInfo.FactoryId);
                updateStringparameters2.Add("@factory_code", cartStockInfo.FactoryCode);
                updateStringparameters2.Add("@enabled", "Y");
                updateStringparameters2.Add("@create_userid", loginId);
                updateStringparameters2.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters2.Add("@guid", Guid.NewGuid());
                updateStringparameters2.Add("@rack_num", cartStockInfo.Qty);
                updateStringparameters2.Add("@update_userid", null);
                updateStringparameters2.Add("@update_time", null);

                sqlStrings.Add(insterString2.ToString());
                parameterList.Add(updateStringparameters2);

                //}
                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();
                    return "OK";
                }
                else
                {
                    transaction.Rollback();
                    return "操作失败，请重新尝试！";
                }
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="stockInfo"></param>
        /// <param name="ruId"></param>
        /// <param name="loginId"></param>
        /// <param name="sumQty"></param>
        /// <returns></returns>
        public string UpdateRackPackageInfo(SysStock stockInfo, int ruId, string loginId, int sumQty, int cartRuId)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                StringBuilder updateString1 = new StringBuilder(1024);
                StringBuilder updateString2 = new StringBuilder(1024);

                DynamicParameters updateStringparameters2 = new DynamicParameters();
                updateString2.Append(@"UPDATE [dbo].[wms_rack_package] SET rack_num=@rack_num,update_time=@update_time,update_userid=@update_userid  where ruid=@ruid");
                updateStringparameters2.Add("@rack_num", sumQty);
                updateStringparameters2.Add("@ruid", ruId);
                updateStringparameters2.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters2.Add("@update_userid", loginId);
                sqlStrings.Add(updateString2.ToString());
                parameterList.Add(updateStringparameters2);


                updateStringparameters2 = new DynamicParameters();
                updateString1.Append(@"UPDATE [dbo].[sys_stock] SET locate_id=@locate_id,locate_name=@locate_name,warehouse_id=@warehouse_id,warehouse_name=@warehouse_name,update_time=@update_time,update_userid=@update_userid  where ruid=@ruid");
                updateStringparameters2.Add("@locate_id", stockInfo.LocateId);
                updateStringparameters2.Add("@locate_name", stockInfo.LocateName);
                updateStringparameters2.Add("@warehouse_id", stockInfo.WarehouseId);
                updateStringparameters2.Add("@warehouse_name", stockInfo.WarehouseName);
                updateStringparameters2.Add("@ruid", cartRuId);
                updateStringparameters2.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters2.Add("@update_userid", loginId);
                sqlStrings.Add(updateString1.ToString());
                parameterList.Add(updateStringparameters2);

                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();
                    return "OK";
                }
                else
                {
                    transaction.Rollback();
                    return "操作失败，请重新尝试！";
                }
            }
        }





        public string InsertStockInfo(List<WmsRackPackage_Extend> rackPackageInfo, string loginId)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                for (int i = 0; i < rackPackageInfo.Count; i++)
                {
                    //新增 sys_stock
                    StringBuilder updateString3 = new StringBuilder(1024);
                    updateString3.Append(@" INSERT INTO [dbo].[sys_stock]
                                        ([vendor_id]
                                        ,[vendor_code]
                                        ,[carton_no]
                                        ,[part_id]
                                        ,[part_no]
                                        ,[part_spec]
                                        ,[lot_no]
                                        ,[fix_lot_no]
                                        ,[status]
                                        ,[qty]
                                        ,[snp_qty]
                                        ,[locate_id]
                                        ,[locate_name]
                                        ,[group_no]
                                        ,[erp_warehouse]
                                        ,[date_code]
                                        ,[qms_status]
                                        ,[ref_order_no]
                                        ,[unit]
                                        ,[dock]
                                        ,[warehouse_id]
                                        ,[warehouse_name]
                                        ,[zone_id]
                                        ,[zone_name]
                                        ,[printed]
                                        ,[print_time]
                                        ,[remark]
                                        ,[factory_id]
                                        ,[factory_code]
                                        ,[enabled]
                                        ,[create_userid]
                                        ,[create_time]
                                        ,[update_userid]
                                        ,[update_time]
                                        ,[guid])
                                        VALUES(@vendor_id,@vendor_code,@carton_no,@part_id,@part_no,@part_spec,@lot_no,@fix_lot_no,@status,@qty,@snp_qty
,@locate_id,@locate_name,@group_no,@erp_warehouse,@date_code,@qms_status,@ref_order_no,@unit,@dock,@warehouse_id,@warehouse_name,@zone_id,@zone_name
,@printed,@print_time,@remark,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid )");

                    DynamicParameters updateStringparameters3 = new DynamicParameters();
                    updateStringparameters3.Add("@vendor_id", 0);
                    updateStringparameters3.Add("@vendor_code", null);
                    updateStringparameters3.Add("@carton_no", rackPackageInfo[i].RackNo);
                    updateStringparameters3.Add("@part_id", 0);
                    updateStringparameters3.Add("@part_no", rackPackageInfo[i].PartNo);
                    updateStringparameters3.Add("@part_spec", rackPackageInfo[i].PartSpec);
                    updateStringparameters3.Add("@lot_no", 0);
                    updateStringparameters3.Add("@fix_lot_no", 0);
                    updateStringparameters3.Add("@status", 10);
                    updateStringparameters3.Add("@qty", 1);
                    updateStringparameters3.Add("@snp_qty", 1);
                    updateStringparameters3.Add("@locate_id", rackPackageInfo[i].LocateId);
                    updateStringparameters3.Add("@locate_name", rackPackageInfo[i].LocateName);
                    updateStringparameters3.Add("@group_no", null);
                    updateStringparameters3.Add("@erp_warehouse", null);
                    updateStringparameters3.Add("@date_code", null);
                    updateStringparameters3.Add("@qms_status", null);
                    updateStringparameters3.Add("@ref_order_no", rackPackageInfo[i].RackNo);
                    updateStringparameters3.Add("@unit", "EA");
                    updateStringparameters3.Add("@dock", 0);
                    updateStringparameters3.Add("@warehouse_id", 0);
                    updateStringparameters3.Add("@warehouse_name", null);
                    updateStringparameters3.Add("@zone_id", 0);
                    updateStringparameters3.Add("@zone_name", null);
                    updateStringparameters3.Add("@printed", null);
                    updateStringparameters3.Add("@print_time", null);
                    updateStringparameters3.Add("@remark", null);
                    updateStringparameters3.Add("@factory_id", rackPackageInfo[i].FactoryId);
                    updateStringparameters3.Add("@factory_code", rackPackageInfo[i].FactoryCode);
                    updateStringparameters3.Add("@enabled", "Y");
                    updateStringparameters3.Add("@update_userid", null);
                    updateStringparameters3.Add("@update_time", null);
                    updateStringparameters3.Add("@create_userid", loginId);
                    updateStringparameters3.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateStringparameters3.Add("@guid", Guid.NewGuid());

                    sqlStrings.Add(updateString3.ToString());
                    parameterList.Add(updateStringparameters3);

                }
                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();
                    return "OK";
                }
                else
                {
                    transaction.Rollback();
                    return "操作失败，请重新尝试！";
                }
            }
        }


        public string UpdateStockStatus(List<SysStock_Extend> oldStock, List<SysLocate> locate, int status, int TransType, string loginId)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                for (int i = 0; i < oldStock.Count; i++)
                {
                    //更新条码表，状态 50 改变为 50 已上架
                    StringBuilder updateString = new StringBuilder(1024);
                    updateString.Append("update sys_stock set status=@status,zone_id=@zone_id,zone_name=@zone_name,locate_id=@locate_id,locate_name=@locate_name, update_userid=@update_userid,update_time=@update_time   where guid=@guid ");
                    DynamicParameters updateStringparameters = new DynamicParameters();
                    updateStringparameters.Add("@status", status);
                    updateStringparameters.Add("@locate_id", locate[0].LocateId);
                    updateStringparameters.Add("@locate_name", locate[0].LocateName);
                    updateStringparameters.Add("@zone_id", locate[0].ZoneId);
                    updateStringparameters.Add("@zone_name", locate[0].ZoneName);
                    updateStringparameters.Add("@guid", oldStock[i].Guid);
                    updateStringparameters.Add("@update_userid", loginId);
                    updateStringparameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    sqlStrings.Add(updateString.ToString());
                    parameterList.Add(updateStringparameters);

                    //新增 sys_stock_trans 事务表
                    StringBuilder updateString4 = new StringBuilder(1024);
                    updateString4.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                    DynamicParameters updateStringparameters4 = new DynamicParameters();
                    updateStringparameters4.Add("@trans_code", TransType);
                    updateStringparameters4.Add("@carton_no", oldStock[i].CartonNo);
                    updateStringparameters4.Add("@part_id", oldStock[i].PartId);
                    updateStringparameters4.Add("@part_no", oldStock[i].PartNo);
                    updateStringparameters4.Add("@part_spec", oldStock[i].PartSpec);
                    updateStringparameters4.Add("@src_locate_id", oldStock[i].LocateId);
                    updateStringparameters4.Add("@src_locate_name", oldStock[i].LocateName);
                    updateStringparameters4.Add("@dest_locate_id", locate[0].LocateId);
                    updateStringparameters4.Add("@dest_locate_name", locate[0].LocateName);
                    updateStringparameters4.Add("@old_qty", oldStock[i].Qty);
                    updateStringparameters4.Add("@new_qty", oldStock[i].Qty);
                    updateStringparameters4.Add("@trans_qty", oldStock[i].Qty);
                    updateStringparameters4.Add("@old_status", oldStock[i].Status);
                    updateStringparameters4.Add("@new_status", status);
                    updateStringparameters4.Add("@old_qms_status", oldStock[i].QmsStatus);
                    updateStringparameters4.Add("@new_qms_status", oldStock[i].QmsStatus);
                    updateStringparameters4.Add("@unit", oldStock[i].Unit);
                    updateStringparameters4.Add("@factory_id", oldStock[i].FactoryId);
                    updateStringparameters4.Add("@factory_code", oldStock[i].FactoryCode);
                    updateStringparameters4.Add("@src_erp_warehouse", oldStock[i].ErpWarehouse);
                    updateStringparameters4.Add("@dest_erp_warehouse", oldStock[i].ErpWarehouse);
                    updateStringparameters4.Add("@src_warehouse_id", oldStock[i].WarehouseId);
                    updateStringparameters4.Add("@src_warehouse_name", oldStock[i].WarehouseName);
                    updateStringparameters4.Add("@dest_warehouse_id", locate[0].WarehouseId);
                    updateStringparameters4.Add("@dest_warehouse_name", locate[0].WarehouseName);
                    updateStringparameters4.Add("@src_zone_id", oldStock[i].ZoneId);
                    updateStringparameters4.Add("@src_zone_name", oldStock[i].ZoneName);
                    updateStringparameters4.Add("@dest_zone_id", locate[0].ZoneId);
                    updateStringparameters4.Add("@dest_zone_name", locate[0].ZoneName);
                    updateStringparameters4.Add("@enabled", oldStock[i].Enabled);
                    updateStringparameters4.Add("@update_userid", loginId);
                    updateStringparameters4.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateStringparameters4.Add("@create_userid", loginId);
                    updateStringparameters4.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateStringparameters4.Add("@guid", Guid.NewGuid());

                    sqlStrings.Add(updateString4.ToString());
                    parameterList.Add(updateStringparameters4);

                }
                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();
                    return "OK";
                }
                else
                {
                    transaction.Rollback();
                    return "操作失败，请重新尝试！";
                }
            }
        }

        public string UpdateStockStatusByMoveOrder(List<SysStock> oldStock, List<SysLocate> locate, int status, int TransType, string loginId, string orderNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                for (int i = 0; i < oldStock.Count; i++)
                {
                    //更新条码表，状态 50 改变为 50 已上架
                    StringBuilder updateString = new StringBuilder(1024);
                    updateString.Append("update sys_stock set status=@status,erp_warehouse=@erp_warehouse,zone_id=@zone_id,zone_name=@zone_name,locate_id=@locate_id,locate_name=@locate_name, update_userid=@update_userid,update_time=@update_time   where guid=@guid ");
                    DynamicParameters updateStringparameters = new DynamicParameters();
                    updateStringparameters.Add("@status", status);
                    updateStringparameters.Add("@locate_id", locate[0].LocateId);
                    updateStringparameters.Add("@locate_name", locate[0].LocateName);
                    updateStringparameters.Add("@zone_id", locate[0].ZoneId);
                    updateStringparameters.Add("@zone_name", locate[0].ZoneName);
                    updateStringparameters.Add("@erp_warehouse", locate[0].ErpWarehouse);
                    updateStringparameters.Add("@guid", oldStock[i].Guid);
                    updateStringparameters.Add("@update_userid", loginId);
                    updateStringparameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    sqlStrings.Add(updateString.ToString());
                    parameterList.Add(updateStringparameters);

                    //新增 sys_stock_trans 事务表
                    StringBuilder updateString4 = new StringBuilder(1024);
                    updateString4.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                    DynamicParameters updateStringparameters4 = new DynamicParameters();
                    updateStringparameters4.Add("@trans_code", TransType);
                    updateStringparameters4.Add("@carton_no", oldStock[i].CartonNo);
                    updateStringparameters4.Add("@part_id", oldStock[i].PartId);
                    updateStringparameters4.Add("@part_no", oldStock[i].PartNo);
                    updateStringparameters4.Add("@part_spec", oldStock[i].PartSpec);
                    updateStringparameters4.Add("@src_locate_id", oldStock[i].LocateId);
                    updateStringparameters4.Add("@src_locate_name", oldStock[i].LocateName);
                    updateStringparameters4.Add("@dest_locate_id", locate[0].LocateId);
                    updateStringparameters4.Add("@dest_locate_name", locate[0].LocateName);
                    updateStringparameters4.Add("@old_qty", oldStock[i].Qty);
                    updateStringparameters4.Add("@new_qty", oldStock[i].Qty);
                    updateStringparameters4.Add("@trans_qty", oldStock[i].Qty);
                    updateStringparameters4.Add("@old_status", oldStock[i].Status);
                    updateStringparameters4.Add("@new_status", status);
                    updateStringparameters4.Add("@old_qms_status", oldStock[i].QmsStatus);
                    updateStringparameters4.Add("@new_qms_status", oldStock[i].QmsStatus);
                    updateStringparameters4.Add("@unit", oldStock[i].Unit);
                    updateStringparameters4.Add("@factory_id", oldStock[i].FactoryId);
                    updateStringparameters4.Add("@factory_code", oldStock[i].FactoryCode);
                    updateStringparameters4.Add("@src_erp_warehouse", oldStock[i].ErpWarehouse);
                    updateStringparameters4.Add("@dest_erp_warehouse", locate[0].ErpWarehouse);
                    updateStringparameters4.Add("@src_warehouse_id", oldStock[i].WarehouseId);
                    updateStringparameters4.Add("@src_warehouse_name", oldStock[i].WarehouseName);
                    updateStringparameters4.Add("@dest_warehouse_id", locate[0].WarehouseId);
                    updateStringparameters4.Add("@dest_warehouse_name", locate[0].WarehouseName);
                    updateStringparameters4.Add("@src_zone_id", oldStock[i].ZoneId);
                    updateStringparameters4.Add("@src_zone_name", oldStock[i].ZoneName);
                    updateStringparameters4.Add("@dest_zone_id", locate[0].ZoneId);
                    updateStringparameters4.Add("@dest_zone_name", locate[0].ZoneName);
                    updateStringparameters4.Add("@enabled", oldStock[i].Enabled);
                    updateStringparameters4.Add("@update_userid", loginId);
                    updateStringparameters4.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateStringparameters4.Add("@create_userid", loginId);
                    updateStringparameters4.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateStringparameters4.Add("@guid", Guid.NewGuid());

                    sqlStrings.Add(updateString4.ToString());
                    parameterList.Add(updateStringparameters4);

                    //移动单主表
                    StringBuilder updateString2 = new StringBuilder(1024);
                    updateString2.Append("update wms_move_header set order_status =@status ,update_userid=@update_userid,update_time=@update_time where order_no = @order_no ");
                    DynamicParameters updateStringparameters2 = new DynamicParameters();
                    updateStringparameters2.Add("@status", (int)WmsEnumUtil.MoveOrderStatus.PROCESSING);
                    updateStringparameters2.Add("@order_no", orderNo);
                    updateStringparameters2.Add("@update_userid", loginId);
                    updateStringparameters2.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    sqlStrings.Add(updateString2.ToString());
                    parameterList.Add(updateStringparameters2);

                    //移动单从表
                    StringBuilder updateString3 = new StringBuilder(1024);
                    updateString3.Append("update wms_move_detail set  out_qty =out_qty + @out_qty,item_status = case when qty =out_qty + @out_qty then @status40 else @status30 end ,update_userid=@update_userid,update_time=@update_time where order_no = @order_no and  part_id = @part_id");
                    DynamicParameters updateStringparameters3 = new DynamicParameters();
                    updateStringparameters3.Add("@status30", (int)WmsEnumUtil.MoveOrderDetailStatus.BATCHING);
                    updateStringparameters3.Add("@status40", (int)WmsEnumUtil.MoveOrderDetailStatus.COMPLETED);
                    updateStringparameters3.Add("@out_qty", oldStock[0].Qty);
                    updateStringparameters3.Add("@part_id", oldStock[0].PartId);
                    updateStringparameters3.Add("@order_no", orderNo);
                    updateStringparameters3.Add("@update_userid", loginId);
                    updateStringparameters3.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    sqlStrings.Add(updateString3.ToString());
                    parameterList.Add(updateStringparameters3);


                    StringBuilder updateString5 = new StringBuilder(1024);
                    updateString5.Append("INSERT INTO dbo.wms_move_sn ");
                    updateString5.Append("( ");
                    updateString5.Append("    order_no,item_no,part_id,part_no,part_spec, ");
                    updateString5.Append("    carton_no,qty,unit,status,factory_id,factory_code,enabled, ");
                    updateString5.Append("    create_userid,create_time,update_userid,update_time,guid ");
                    updateString5.Append(") ");
                    updateString5.Append("VALUES ");
                    updateString5.Append("(   @orderNo,@itemNo,@partId,@partNo,@partSpec, ");
                    updateString5.Append("    @cartonNo,@qty,@unit,@status,@factoryId,@factoryCode,@enabled, ");
                    updateString5.Append("    @createUserid,@createTime,@updateUserid,@updateTime,@guid ");
                    updateString5.Append("    ) ");

                    DynamicParameters updateStringparameters5 = new DynamicParameters();
                    updateStringparameters5.Add("@orderNo", orderNo);
                    updateStringparameters5.Add("@itemNo", "1");
                    updateStringparameters5.Add("@partId", oldStock[0].PartId);
                    updateStringparameters5.Add("@partNo", oldStock[0].PartNo);
                    updateStringparameters5.Add("@partSpec", oldStock[0].PartSpec);
                    updateStringparameters5.Add("@cartonNo", oldStock[0].CartonNo);
                    updateStringparameters5.Add("@qty", oldStock[0].Qty);
                    updateStringparameters5.Add("@unit", oldStock[0].Unit);
                    updateStringparameters5.Add("@status", (int)WmsEnumUtil.MoveOrderSnStatus.CREATE);
                    updateStringparameters5.Add("@factoryId", oldStock[0].FactoryId);
                    updateStringparameters5.Add("@factoryCode", oldStock[0].FactoryCode);
                    updateStringparameters5.Add("@enabled", WmsEnumUtil.Enabled.Y.ToString());
                    updateStringparameters5.Add("@createUserid", loginId);
                    updateStringparameters5.Add("@createTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    updateStringparameters5.Add("@updateUserid", loginId);
                    updateStringparameters5.Add("@updateTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    updateStringparameters5.Add("@guid", Guid.NewGuid());

                    sqlStrings.Add(updateString5.ToString());
                    parameterList.Add(updateStringparameters5);

                }
                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();
                    return "OK";
                }
                else
                {
                    transaction.Rollback();
                    return "操作失败，请重新尝试！";
                }
            }
        }

        public List<SysStock> GetCartonNoInfoByPack(string cartonNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("select * from sys_stock (nolock) where group_no= (select group_no from sys_stock (nolock) where carton_no = @carton_no )  and status=@status  ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@carton_no", cartonNo);
                parameters.Add("@status", (int)WmsEnumUtil.StockStatus.INSTOCKED);
                return dbConn.Query<SysStock>(sql.ToString(), parameters).ToList();
            }
        }

        public List<SysStock> GetCartonNoInfo(string cartonNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("select * from sys_stock (nolock) where carton_no= @carton_no and enabled='Y'");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@carton_no", cartonNo);

                return dbConn.Query<SysStock>(sql.ToString(), parameters).ToList();
            }
        }


        public List<WmsRackPackage_Extend> GetCartonInfo(string rackNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("select * from wms_rack_package (nolock) where rack_no= @rack_no and enabled='Y' ORDER BY create_time DESC");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@rack_no", rackNo);

                return dbConn.Query<WmsRackPackage_Extend>(sql.ToString(), parameters).ToList();
            }
        }


        public List<WmsRackPackage_Extend> GetRackCartonList(string rackNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("SELECT  p.* FROM dbo.wms_rack_package p  RIGHT JOIN dbo.sys_stock s ON p.corton_no=s.carton_no AND s.carton_type=0 AND s.enabled='Y' WHERE p.rack_no=@rack_no    union   SELECT p.* FROM dbo.wms_rack_package p  LEFT JOIN dbo.wms_rack r ON r.rack_no=p.corton_no AND r.enabled='Y' WHERE  p.enabled='Y' AND p.rack_no IN (SELECT p.corton_no FROM dbo.wms_rack_package p  RIGHT JOIN dbo.wms_rack r ON r.rack_no=p.corton_no AND r.enabled='Y' WHERE p.rack_no=@rack_no  AND p.enabled='Y') ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@rack_no", rackNo);
                return dbConn.Query<WmsRackPackage_Extend>(sql.ToString(), parameters).ToList();
            }
        }

        public List<WmsRackPackage_Extend> GetTorrRackList(string rackNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("SELECT * FROM dbo.wms_rack_package p  RIGHT JOIN dbo.wms_rack r ON r.rack_no=p.corton_no AND r.enabled='Y' WHERE p.rack_no=@rack_no AND p.enabled='Y'");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@rack_no", rackNo);

                return dbConn.Query<WmsRackPackage_Extend>(sql.ToString(), parameters).ToList();
            }
        }


        public List<WmsMoveHeader> GetMoveOrderList(string cartonno)
        {
            IDbConnection dbConn = dapperDbContext.GetDbConnection();

            StringBuilder sql = new StringBuilder(1024);

            if (cartonno == null || cartonno == "")
            {
                sql.Append("select * from wms_move_header (nolock) where order_type = @order_type and order_status in (@status10,@status20,@status30) order by create_time desc ");
            }
            else
            {
                sql.Append("select * from wms_move_header (nolock) where order_no like '%"+cartonno+"%' and  order_type = @order_type and order_status in (@status10,@status20,@status30) order by create_time desc ");
            }
                
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("@order_type", (int)WmsEnumUtil.MoveOrderType.TRANSFER_ORDER);
            parameters.Add("@status10", (int)WmsEnumUtil.MoveOrderDetailStatus.CREATE);
            parameters.Add("@status20", (int)WmsEnumUtil.MoveOrderDetailStatus.GENERATED);
            parameters.Add("@status30", (int)WmsEnumUtil.MoveOrderDetailStatus.BATCHING);

            List<WmsMoveHeader> xxx = dbConn.Query<WmsMoveHeader>(sql.ToString(), parameters).ToList();
            return xxx;
        }

        public List<WmsMoveDetail> CheckMoveOrderLocate(string orderNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("select * from wms_move_detail (nolock) where order_no = @order_no and enabled='Y'");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@order_no", orderNo);

                return dbConn.Query<WmsMoveDetail>(sql.ToString(), parameters).ToList();
            }
        }


        public List<WmsRackPackage_Extend> GetRackPartNo(string rackNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("select * from wms_rack_package (nolock) where rack_no = @rack_no and enabled='Y'");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@rack_no", rackNo);

                return dbConn.Query<WmsRackPackage_Extend>(sql.ToString(), parameters).ToList();
            }
        }



        public string ChangeMoveHeaderStatus(string loginId, string orderNo)
        {
            IDbConnection dbConn = dapperDbContext.GetDbConnection();

            StringBuilder sql = new StringBuilder(1024);
            sql.Append("update wms_move_header set order_status = @order_status , update_userid=@update_userid,update_time=@update_time  where order_no = @order_no");
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("@order_no", orderNo);
            parameters.Add("@order_status", (int)WmsEnumUtil.MoveOrderStatus.COMPLETED);
            parameters.Add("@update_userid", loginId);
            parameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            dbConn.Execute(sql.ToString(), parameters);

            return "OK";

        }

        public List<WmsMoveDetail_Extend> GetMoveDetailInfo(string orderNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                StringBuilder getqcSQL = new StringBuilder(1024);
                getqcSQL.Append(@"SELECT * FROM dbo.wms_move_detail (NOLOCK) WHERE order_no=@order_no AND enabled='Y'");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@order_no", orderNo);
                //parameters.Add("@status", status);
                List<WmsMoveDetail_Extend> WmsMoveDetail = dbConn.Query<WmsMoveDetail_Extend>(getqcSQL.ToString(), parameters).ToList();

                return WmsMoveDetail;
            }
        }

        public SysStock GetStockCartonInfo(string cartonNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("  SELECT * FROM dbo.sys_stock(NOLOCK) WHERE  enabled='Y' AND carton_no IN (  SELECT rack_no FROM dbo.wms_rack WHERE  enabled='Y' AND packageType=1 AND rack_no=@carton_no) ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@carton_no", cartonNo);

                return dbConn.QueryFirstOrDefault<SysStock>(sql.ToString(), parameters);
            }
        }

        public SysStock_Extend GetCartonNoInfo2(string cartonNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("select * from sys_stock (nolock) where carton_no= @carton_no and enabled='Y'");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@carton_no", cartonNo);

                return dbConn.QueryFirstOrDefault<SysStock_Extend>(sql.ToString(), parameters);
            }
        }


        public SysLocate GetLocateZTbin()
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("  SELECT * FROM dbo.sys_locate WHERE locate_desc LIKE '%在途%' AND enabled='Y'  ");
                DynamicParameters parameters = new DynamicParameters();
                //parameters.Add("@carton_no", cartonNo);

                return dbConn.QueryFirstOrDefault<SysLocate>(sql.ToString(), parameters);
            }
        }



        public WmsRackPackage_Extend GetpackageInfo(string cortonNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("select * from wms_rack_package (nolock) where corton_no= @corton_no and enabled='Y'");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@corton_no", cortonNo);

                return dbConn.QueryFirstOrDefault<WmsRackPackage_Extend>(sql.ToString(), parameters);
            }
        }

        public string UpdatepackageInfo(string cortonNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;
                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                StringBuilder updateString1 = new StringBuilder(1024);

                DynamicParameters updateStringparameters2 = new DynamicParameters();

                updateString1.Append("UPDATE wms_rack_package SET enabled='N' WHERE corton_no=@corton_no AND enabled='Y'");
                DynamicParameters parameters = new DynamicParameters();
                updateStringparameters2.Add("@corton_no", cortonNo);

                sqlStrings.Add(updateString1.ToString());
                parameterList.Add(updateStringparameters2);

                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();
                    return "OK";
                }
                else
                {
                    transaction.Rollback();
                    return "操作失败，请重新尝试！";
                }
            }
        }


        public SysStock GetSplitCartonInfo(string cartonNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("select * from sys_stock (nolock) where carton_no=@carton_no ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@carton_no", cartonNo);

                //List<SysStock> dt = dbConn.Query<SysStock>(sql.ToString(), parameters).ToList();
                return dbConn.QueryFirstOrDefault<SysStock>(sql.ToString(), parameters);
            }
        }

        public List<SysStock> UpdatePickingLocate(WmsRackPackage_Extend rackPackageInfo,int? locateId, string locateCode, string rackNo,List<SysStock> oldStock, int splitNum, string loginId)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                //更新条码表，库位、状态
                StringBuilder updateString = new StringBuilder(1024);
                updateString.Append("UPDATE dbo.sys_stock SET locate_id=@locate_id ,locate_name=@locate_name ,update_userid=@update_userid ,update_time=@update_time  WHERE carton_no=@carton_no ");
                DynamicParameters updateStringparameters = new DynamicParameters();
                updateStringparameters.Add("@locate_id", locateId);
                updateStringparameters.Add("@locate_name", locateCode);
                updateStringparameters.Add("@carton_no", oldStock[0].CartonNo);
                updateStringparameters.Add("@update_userid", loginId);
                updateStringparameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                sqlStrings.Add(updateString.ToString());
                parameterList.Add(updateStringparameters);


                if (rackPackageInfo != null)
                {
                    //更新周转箱中翻包的条码
                    StringBuilder updateString2 = new StringBuilder(1024);
                    updateString2.Append("UPDATE dbo.wms_rack_package SET rack_no=@rack_no ,update_userid=@update_userid ,update_time=@update_time  WHERE ruid=@ruid ");
                    DynamicParameters updateStringparameters2 = new DynamicParameters();
                    updateStringparameters2.Add("@rack_no", rackNo);
                    updateStringparameters2.Add("@ruid", rackPackageInfo.Ruid);
                    updateStringparameters2.Add("@update_userid", loginId);
                    updateStringparameters2.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    sqlStrings.Add(updateString2.ToString());
                    parameterList.Add(updateStringparameters2);
                }
                

                //新增 sys_stock_trans 事务表
                #region sys_stock_trans 事务表

                StringBuilder updateString4 = new StringBuilder(1024);
                updateString4.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                DynamicParameters updateStringparameters4 = new DynamicParameters();
                updateStringparameters4.Add("@trans_code", (int)WmsEnumUtil.TransType.PART_SPLIT);
                updateStringparameters4.Add("@carton_no", oldStock[0].CartonNo);
                updateStringparameters4.Add("@part_id", oldStock[0].PartId);
                updateStringparameters4.Add("@part_no", oldStock[0].PartNo);
                updateStringparameters4.Add("@part_spec", oldStock[0].PartSpec);
                updateStringparameters4.Add("@src_locate_id", oldStock[0].LocateId);
                updateStringparameters4.Add("@src_locate_name", oldStock[0].LocateName);
                updateStringparameters4.Add("@dest_locate_id", locateId);
                updateStringparameters4.Add("@dest_locate_name", locateCode);
                updateStringparameters4.Add("@old_qty", oldStock[0].Qty);
                updateStringparameters4.Add("@new_qty", oldStock[0].Qty);
                updateStringparameters4.Add("@trans_qty", oldStock[0].Qty);
                updateStringparameters4.Add("@old_status", oldStock[0].Status);
                updateStringparameters4.Add("@new_status", oldStock[0].Status);
                updateStringparameters4.Add("@old_qms_status", oldStock[0].QmsStatus);
                updateStringparameters4.Add("@new_qms_status", oldStock[0].QmsStatus);
                updateStringparameters4.Add("@unit", oldStock[0].Unit);
                updateStringparameters4.Add("@factory_id", oldStock[0].FactoryId);
                updateStringparameters4.Add("@factory_code", oldStock[0].FactoryCode);
                updateStringparameters4.Add("@src_erp_warehouse", oldStock[0].ErpWarehouse);
                updateStringparameters4.Add("@dest_erp_warehouse", oldStock[0].ErpWarehouse);
                updateStringparameters4.Add("@src_warehouse_id", oldStock[0].WarehouseId);
                updateStringparameters4.Add("@src_warehouse_name", oldStock[0].WarehouseName);
                updateStringparameters4.Add("@dest_warehouse_id", oldStock[0].WarehouseId);
                updateStringparameters4.Add("@dest_warehouse_name", oldStock[0].WarehouseName);
                updateStringparameters4.Add("@src_zone_id", oldStock[0].ZoneId);
                updateStringparameters4.Add("@src_zone_name", oldStock[0].ZoneName);
                updateStringparameters4.Add("@dest_zone_id", oldStock[0].ZoneId);
                updateStringparameters4.Add("@dest_zone_name", oldStock[0].ZoneName);
                updateStringparameters4.Add("@enabled", oldStock[0].Enabled);
                updateStringparameters4.Add("@update_userid", loginId);
                updateStringparameters4.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters4.Add("@create_userid", loginId);
                updateStringparameters4.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters4.Add("@guid", Guid.NewGuid());

                sqlStrings.Add(updateString4.ToString());
                parameterList.Add(updateStringparameters4);
                #endregion



                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();
                    return oldStock;
                }
                else
                {
                    transaction.Rollback();
                    return null;
                }
            }
        }




        public List<SysStock> UpdateReturnMaterialInfo(string orderNo, List<WmsMoveDetail_Extend> wmsMoveDetails,SysStock_Extend stockInfo, int? locateId, string locateCode,int zoneId ,string zoneCode, string rackNo, List<SysStock> oldStock, int splitNum, string loginId, int splitCount)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                decimal oldQty = oldStock[0].Qty;
                decimal newQty ;
                if (oldStock[0].Qty == 0)
                {
                    newQty = 0;
                }
                else {
                    newQty = oldStock[0].Qty - splitNum;
                }

                //更新条码表，数量
                StringBuilder updateString = new StringBuilder(1024);
                updateString.Append("update sys_stock set qty=@qty,relevance_order=@relevance_order  where carton_no=@carton_no ");
                DynamicParameters updateStringparameters = new DynamicParameters();
                updateStringparameters.Add("@qty", newQty);
                updateStringparameters.Add("@relevance_order", orderNo);
                updateStringparameters.Add("@carton_no", oldStock[0].CartonNo);
                updateStringparameters.Add("@update_userid", loginId);
                updateStringparameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                sqlStrings.Add(updateString.ToString());
                parameterList.Add(updateStringparameters);


                //新增 sys_stock_trans 事务表
                #region sys_stock_trans 事务表

                StringBuilder updateString4 = new StringBuilder(1024);
                updateString4.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                DynamicParameters updateStringparameters4 = new DynamicParameters();
                updateStringparameters4.Add("@trans_code", (int)WmsEnumUtil.TransType.PART_SPLIT);
                updateStringparameters4.Add("@carton_no", oldStock[0].CartonNo);
                updateStringparameters4.Add("@part_id", oldStock[0].PartId);
                updateStringparameters4.Add("@part_no", oldStock[0].PartNo);
                updateStringparameters4.Add("@part_spec", oldStock[0].PartSpec);
                updateStringparameters4.Add("@src_locate_id", oldStock[0].LocateId);
                updateStringparameters4.Add("@src_locate_name", oldStock[0].LocateName);
                updateStringparameters4.Add("@dest_locate_id", oldStock[0].LocateId);
                updateStringparameters4.Add("@dest_locate_name", oldStock[0].LocateName);
                updateStringparameters4.Add("@old_qty", oldQty);
                updateStringparameters4.Add("@new_qty", newQty);
                updateStringparameters4.Add("@trans_qty", oldStock[0].Qty);
                updateStringparameters4.Add("@old_status", oldStock[0].Status);
                updateStringparameters4.Add("@new_status", oldStock[0].Status);
                updateStringparameters4.Add("@old_qms_status", oldStock[0].QmsStatus);
                updateStringparameters4.Add("@new_qms_status", oldStock[0].QmsStatus);
                updateStringparameters4.Add("@unit", oldStock[0].Unit);
                updateStringparameters4.Add("@factory_id", oldStock[0].FactoryId);
                updateStringparameters4.Add("@factory_code", oldStock[0].FactoryCode);
                updateStringparameters4.Add("@src_erp_warehouse", oldStock[0].ErpWarehouse);
                updateStringparameters4.Add("@dest_erp_warehouse", oldStock[0].ErpWarehouse);
                updateStringparameters4.Add("@src_warehouse_id", oldStock[0].WarehouseId);
                updateStringparameters4.Add("@src_warehouse_name", oldStock[0].WarehouseName);
                updateStringparameters4.Add("@dest_warehouse_id", oldStock[0].WarehouseId);
                updateStringparameters4.Add("@dest_warehouse_name", oldStock[0].WarehouseName);
                updateStringparameters4.Add("@src_zone_id", oldStock[0].ZoneId);
                updateStringparameters4.Add("@src_zone_name", oldStock[0].ZoneName);
                updateStringparameters4.Add("@dest_zone_id", oldStock[0].ZoneId);
                updateStringparameters4.Add("@dest_zone_name", oldStock[0].ZoneName);
                updateStringparameters4.Add("@enabled", oldStock[0].Enabled);
                updateStringparameters4.Add("@update_userid", loginId);
                updateStringparameters4.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters4.Add("@create_userid", loginId);
                updateStringparameters4.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters4.Add("@guid", Guid.NewGuid());

                sqlStrings.Add(updateString4.ToString());
                parameterList.Add(updateStringparameters4);
                #endregion

                oldStock[0].CartonNo = checkCartonNo(oldStock[0].CartonNo, splitCount);
                oldStock[0].Qty = splitNum;


                if (wmsMoveDetails.Count > 0)
                {
                    for (int i = 0; i < wmsMoveDetails.Count; i++)
                    {
                        StringBuilder updateString2 = new StringBuilder(1024);
                        updateString2.Append(@"update wms_move_detail set dest_locate_id=@dest_locate_id,dest_locate_name=@dest_locate_name,dest_zone_id=@dest_zone_id,dest_zone_name=@dest_zone_name,pick_qty = pick_qty + @pick_qty,out_qty=out_qty+@out_qty, item_status=@item_status,update_userid=@update_userid,update_time=@update_time where ruid=@ruid ");
                        DynamicParameters updateStringparameters2 = new DynamicParameters();
                        updateStringparameters2.Add("@item_status", (int)WmsEnumUtil.MoveOrderDetailStatus.BATCHING);
                        updateStringparameters2.Add("@ruid", wmsMoveDetails[i].Ruid);
                        updateStringparameters2.Add("@dest_locate_id", locateId);
                        updateStringparameters2.Add("@dest_locate_name", locateCode);
                        updateStringparameters2.Add("@dest_zone_id", zoneId);
                        updateStringparameters2.Add("@dest_zone_name", zoneCode);
                        updateStringparameters2.Add("@pick_qty", splitNum);
                        updateStringparameters2.Add("@out_qty", splitNum);
                        updateStringparameters2.Add("@update_userid", loginId);
                        updateStringparameters2.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                        sqlStrings.Add(updateString2.ToString());
                        parameterList.Add(updateStringparameters2);



                        //插入 wms_move_sn 条码表
                        StringBuilder updateString8 = new StringBuilder(1024);
                        updateString8.Append(@"INSERT INTO [dbo].[wms_move_sn]
                                    ([order_no]
                                    ,[item_no]
                                    ,[part_id]
                                    ,[part_no]
                                    ,[part_spec]
                                    ,[carton_no]
                                    ,[qty]
                                    ,[unit]
                                    ,[status]
                                    ,[factory_id]
                                    ,[factory_code]
                                    ,[enabled]
                                    ,[create_userid]
                                    ,[create_time]
                                    ,[update_userid]
                                    ,[update_time]
                                    ,[guid])
                                     VALUES (@order_no,@item_no,@part_id,@part_no,@part_spec,@carton_no,@qty,@unit,@status,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid)");
                        DynamicParameters updateStringparameters8 = new DynamicParameters();
                        updateStringparameters8.Add("@order_no", wmsMoveDetails[i].OrderNo);
                        updateStringparameters8.Add("@item_no", wmsMoveDetails[i].ItemNo);
                        updateStringparameters8.Add("@part_id", wmsMoveDetails[i].PartId);
                        updateStringparameters8.Add("@part_no", wmsMoveDetails[i].PartNo);
                        updateStringparameters8.Add("@part_spec", wmsMoveDetails[i].PartSpec);
                        updateStringparameters8.Add("@carton_no", oldStock[0].CartonNo);
                        updateStringparameters8.Add("@qty", splitNum);
                        updateStringparameters8.Add("@unit", oldStock[0].Unit);
                        updateStringparameters8.Add("@status", (int)WmsEnumUtil.MoveOrderSnStatus.BATCHED);
                        updateStringparameters8.Add("@factory_id", wmsMoveDetails[i].FactoryId);
                        updateStringparameters8.Add("@factory_code", wmsMoveDetails[i].FactoryCode);
                        updateStringparameters8.Add("@enabled", "Y");
                        updateStringparameters8.Add("@update_userid", loginId);
                        updateStringparameters8.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        updateStringparameters8.Add("@create_userid", loginId);
                        updateStringparameters8.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        updateStringparameters8.Add("@guid", Guid.NewGuid());

                        sqlStrings.Add(updateString8.ToString());
                        parameterList.Add(updateStringparameters8);
                    }
                }


                //if (packlist.Count != 0)
                //{
                //    int intRackNmu = Convert.ToInt32(packlist.FirstOrDefault().RackNum);
                //    int rackNum = intRackNmu + splitNum;
                //    //更新周转箱明细中的数量
                //    StringBuilder updateString2 = new StringBuilder(1024);
                //    updateString2.Append("UPDATE dbo.wms_rack_package SET rack_num = @rack_num ,update_userid = @update_userid ,update_time = @update_time WHERE ruid = @ruid ");
                //    DynamicParameters updateStringparameters9 = new DynamicParameters();
                //    updateStringparameters9.Add("@rack_num", rackNum);
                //    updateStringparameters9.Add("@carton_no", packlist.FirstOrDefault().Ruid);
                //    updateStringparameters9.Add("@update_userid", loginId);
                //    updateStringparameters9.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                //    sqlStrings.Add(updateString2.ToString());
                //    parameterList.Add(updateStringparameters9);
                //}
                //else
                //{
                ////插入 wms_rack_package 周转箱明细表
                //StringBuilder insterString2 = new StringBuilder(1024);
                //DynamicParameters updateStringparameters7 = new DynamicParameters();
                //insterString2.Append(@"INSERT INTO [dbo].[wms_rack_package]
                //                 ([rack_no]
                //                 ,[corton_no]
                //                 ,[packageType]
                //                 ,[part_no]
                //                 ,[factory_id]
                //                 ,[factory_code]
                //                 ,[enabled]
                //                 ,[create_userid]
                //                 ,[create_time]
                //                 ,[update_userid]
                //                 ,[update_time]
                //                 ,[guid],[rack_num])
                //           VALUES (@rack_no,@corton_no,@pack_type,@part_no,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid,@rack_num)");
                //updateStringparameters7 = new DynamicParameters();
                //updateStringparameters7.Add("@rack_no", rackNo);
                //updateStringparameters7.Add("@corton_no", oldStock[0].CartonNo);
                //updateStringparameters7.Add("@pack_type", 0);
                //updateStringparameters7.Add("@part_no", oldStock[0].PartNo);
                //updateStringparameters7.Add("@factory_id", oldStock[0].FactoryId);
                //updateStringparameters7.Add("@factory_code", oldStock[0].FactoryCode);
                //updateStringparameters7.Add("@enabled", "Y");
                //updateStringparameters7.Add("@create_userid", loginId);
                //updateStringparameters7.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                //updateStringparameters7.Add("@guid", Guid.NewGuid());
                //updateStringparameters7.Add("@rack_num", splitNum);
                //updateStringparameters7.Add("@update_userid", null);
                //updateStringparameters7.Add("@update_time", null);

                //sqlStrings.Add(insterString2.ToString());
                //parameterList.Add(updateStringparameters7);
                //}




                //插入拆分出的箱条码
                #region 插入拆分出的箱条码

                StringBuilder updateString3 = new StringBuilder(1024);
                updateString3.Append(@" INSERT INTO [dbo].[sys_stock]
                                        ([vendor_id]
                                        ,[vendor_code]
                                        ,[carton_no]
                                        ,[part_id]
                                        ,[part_no]
                                        ,[part_spec]
                                        ,[lot_no]
                                        ,[fix_lot_no]
                                        ,[status]
                                        ,[qty]
                                        ,[snp_qty]
                                        ,[locate_id]
                                        ,[locate_name]
                                        ,[group_no]
                                        ,[erp_warehouse]
                                        ,[date_code]
                                        ,[qms_status]
                                        ,[ref_order_no]
                                        ,[unit]
                                        ,[dock]
                                        ,[warehouse_id]
                                        ,[warehouse_name]
                                        ,[zone_id]
                                        ,[zone_name]
                                        ,[printed]
                                        ,[print_time]
                                        ,[remark]
                                        ,[factory_id]
                                        ,[factory_code]
                                        ,[enabled]
                                        ,[create_userid]
                                        ,[create_time]
                                        ,[update_userid]
                                        ,[update_time]
                                        ,[guid])
                                        VALUES(@vendor_id,@vendor_code,@carton_no,@part_id,@part_no,@part_spec,@lot_no,@fix_lot_no,@status,@qty,@snp_qty
,@locate_id,@locate_name,@group_no,@erp_warehouse,@date_code,@qms_status,@ref_order_no,@unit,@dock,@warehouse_id,@warehouse_name,@zone_id,@zone_name
,@printed,@print_time,@remark,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid )");

                DynamicParameters updateStringparameters3 = new DynamicParameters();
                updateStringparameters3.Add("@vendor_id", oldStock[0].VendorId);
                updateStringparameters3.Add("@vendor_code", oldStock[0].VendorName);
                updateStringparameters3.Add("@carton_no", oldStock[0].CartonNo);
                updateStringparameters3.Add("@part_id", oldStock[0].PartId);
                updateStringparameters3.Add("@part_no", oldStock[0].PartNo);
                updateStringparameters3.Add("@part_spec", oldStock[0].PartSpec);
                updateStringparameters3.Add("@lot_no", oldStock[0].LotNo);
                updateStringparameters3.Add("@fix_lot_no", oldStock[0].FixLotNo);
                updateStringparameters3.Add("@status", 40); //入库
                updateStringparameters3.Add("@qty", splitNum);
                updateStringparameters3.Add("@snp_qty", oldStock[0].SnpQty);
                updateStringparameters3.Add("@locate_id", locateId);
                updateStringparameters3.Add("@locate_name", locateCode); 
                updateStringparameters3.Add("@group_no", oldStock[0].GroupNo);
                updateStringparameters3.Add("@erp_warehouse", oldStock[0].ErpWarehouse);
                updateStringparameters3.Add("@date_code", oldStock[0].DateCode);
                updateStringparameters3.Add("@qms_status", oldStock[0].QmsStatus);
                updateStringparameters3.Add("@ref_order_no", oldStock[0].RefOrderNo);
                updateStringparameters3.Add("@unit", oldStock[0].Unit);
                updateStringparameters3.Add("@dock", oldStock[0].Dock);
                updateStringparameters3.Add("@warehouse_id", oldStock[0].WarehouseId);
                updateStringparameters3.Add("@warehouse_name", oldStock[0].WarehouseName);
                updateStringparameters3.Add("@zone_id", zoneId);
                updateStringparameters3.Add("@zone_name", zoneCode);
                updateStringparameters3.Add("@printed", oldStock[0].Printed);
                updateStringparameters3.Add("@print_time", oldStock[0].PrintTime);
                updateStringparameters3.Add("@remark", oldStock[0].Remark);
                updateStringparameters3.Add("@factory_id", oldStock[0].FactoryId);
                updateStringparameters3.Add("@factory_code", oldStock[0].FactoryCode);
                updateStringparameters3.Add("@enabled", oldStock[0].Enabled);
                updateStringparameters3.Add("@update_userid", loginId);
                updateStringparameters3.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters3.Add("@create_userid", oldStock[0].CreateUserId);
                updateStringparameters3.Add("@create_time", oldStock[0].CreateTime);
                updateStringparameters3.Add("@guid", Guid.NewGuid());

                sqlStrings.Add(updateString3.ToString());
                parameterList.Add(updateStringparameters3);
                #endregion

                //新增 sys_stock_trans 事务表
                #region sys_stock_trans 事务表

                StringBuilder updateString5 = new StringBuilder(1024);
                updateString5.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                DynamicParameters updateStringparameters5 = new DynamicParameters();
                updateStringparameters5.Add("@trans_code", (int)WmsEnumUtil.TransType.PART_SPLIT);
                updateStringparameters5.Add("@carton_no", oldStock[0].CartonNo);
                updateStringparameters5.Add("@part_id", oldStock[0].PartId);
                updateStringparameters5.Add("@part_no", oldStock[0].PartNo);
                updateStringparameters5.Add("@part_spec", oldStock[0].PartSpec);
                updateStringparameters5.Add("@src_locate_id", oldStock[0].LocateId);
                updateStringparameters5.Add("@src_locate_name", oldStock[0].LocateName);
                updateStringparameters5.Add("@dest_locate_id", oldStock[0].LocateId);
                updateStringparameters5.Add("@dest_locate_name", oldStock[0].LocateName);
                updateStringparameters5.Add("@old_qty", 0);
                updateStringparameters5.Add("@new_qty", splitNum);
                updateStringparameters5.Add("@trans_qty", oldStock[0].Qty);
                updateStringparameters5.Add("@old_status", oldStock[0].Status);
                updateStringparameters5.Add("@new_status", oldStock[0].Status);
                updateStringparameters5.Add("@old_qms_status", oldStock[0].QmsStatus);
                updateStringparameters5.Add("@new_qms_status", oldStock[0].QmsStatus);
                updateStringparameters5.Add("@unit", oldStock[0].Unit);
                updateStringparameters5.Add("@factory_id", oldStock[0].FactoryId);
                updateStringparameters5.Add("@factory_code", oldStock[0].FactoryCode);
                updateStringparameters5.Add("@src_erp_warehouse", oldStock[0].ErpWarehouse);
                updateStringparameters5.Add("@dest_erp_warehouse", oldStock[0].ErpWarehouse);
                updateStringparameters5.Add("@src_warehouse_id", oldStock[0].WarehouseId);
                updateStringparameters5.Add("@src_warehouse_name", oldStock[0].WarehouseName);
                updateStringparameters5.Add("@dest_warehouse_id", oldStock[0].WarehouseId);
                updateStringparameters5.Add("@dest_warehouse_name", oldStock[0].WarehouseName);
                updateStringparameters5.Add("@src_zone_id", oldStock[0].ZoneId);
                updateStringparameters5.Add("@src_zone_name", oldStock[0].ZoneName);
                updateStringparameters5.Add("@dest_zone_id", oldStock[0].ZoneId);
                updateStringparameters5.Add("@dest_zone_name", oldStock[0].ZoneName);
                updateStringparameters5.Add("@enabled", oldStock[0].Enabled);
                updateStringparameters5.Add("@update_userid", loginId);
                updateStringparameters5.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters5.Add("@create_userid", loginId);
                updateStringparameters5.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters5.Add("@guid", Guid.NewGuid());

                sqlStrings.Add(updateString5.ToString());
                parameterList.Add(updateStringparameters5);
                #endregion


                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();
                    return oldStock;
                }
                else
                {
                    transaction.Rollback();
                    return null;
                }
            }
        }





        //翻箱-拆分条码
        public List<SysStock> PickingCheckMaterialSplit(int? locateId, string locateCode, string rackNo,  List<SysStock> oldStock, int splitNum, string loginId, int splitCount)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                decimal oldQty = oldStock[0].Qty;
                decimal newQty = oldStock[0].Qty - splitNum;

                //更新条码表，数量
                StringBuilder updateString = new StringBuilder(1024);
                updateString.Append("update sys_stock set qty=@qty  where carton_no=@carton_no ");
                DynamicParameters updateStringparameters = new DynamicParameters();
                updateStringparameters.Add("@qty", newQty);
                updateStringparameters.Add("@carton_no", oldStock[0].CartonNo);
                updateStringparameters.Add("@update_userid", loginId);
                updateStringparameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                sqlStrings.Add(updateString.ToString());
                parameterList.Add(updateStringparameters);


                //新增 sys_stock_trans 事务表
                #region sys_stock_trans 事务表

                StringBuilder updateString4 = new StringBuilder(1024);
                updateString4.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                DynamicParameters updateStringparameters4 = new DynamicParameters();
                updateStringparameters4.Add("@trans_code", (int)WmsEnumUtil.TransType.PART_SPLIT);
                updateStringparameters4.Add("@carton_no", oldStock[0].CartonNo);
                updateStringparameters4.Add("@part_id", oldStock[0].PartId);
                updateStringparameters4.Add("@part_no", oldStock[0].PartNo);
                updateStringparameters4.Add("@part_spec", oldStock[0].PartSpec);
                updateStringparameters4.Add("@src_locate_id", oldStock[0].LocateId);
                updateStringparameters4.Add("@src_locate_name", oldStock[0].LocateName);
                updateStringparameters4.Add("@dest_locate_id", oldStock[0].LocateId);
                updateStringparameters4.Add("@dest_locate_name", oldStock[0].LocateName);
                updateStringparameters4.Add("@old_qty", oldQty);
                updateStringparameters4.Add("@new_qty", newQty);
                updateStringparameters4.Add("@trans_qty", oldStock[0].Qty);
                updateStringparameters4.Add("@old_status", oldStock[0].Status);
                updateStringparameters4.Add("@new_status", oldStock[0].Status);
                updateStringparameters4.Add("@old_qms_status", oldStock[0].QmsStatus);
                updateStringparameters4.Add("@new_qms_status", oldStock[0].QmsStatus);
                updateStringparameters4.Add("@unit", oldStock[0].Unit);
                updateStringparameters4.Add("@factory_id", oldStock[0].FactoryId);
                updateStringparameters4.Add("@factory_code", oldStock[0].FactoryCode);
                updateStringparameters4.Add("@src_erp_warehouse", oldStock[0].ErpWarehouse);
                updateStringparameters4.Add("@dest_erp_warehouse", oldStock[0].ErpWarehouse);
                updateStringparameters4.Add("@src_warehouse_id", oldStock[0].WarehouseId);
                updateStringparameters4.Add("@src_warehouse_name", oldStock[0].WarehouseName);
                updateStringparameters4.Add("@dest_warehouse_id", oldStock[0].WarehouseId);
                updateStringparameters4.Add("@dest_warehouse_name", oldStock[0].WarehouseName);
                updateStringparameters4.Add("@src_zone_id", oldStock[0].ZoneId);
                updateStringparameters4.Add("@src_zone_name", oldStock[0].ZoneName);
                updateStringparameters4.Add("@dest_zone_id", oldStock[0].ZoneId);
                updateStringparameters4.Add("@dest_zone_name", oldStock[0].ZoneName);
                updateStringparameters4.Add("@enabled", oldStock[0].Enabled);
                updateStringparameters4.Add("@update_userid", loginId);
                updateStringparameters4.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters4.Add("@create_userid", loginId);
                updateStringparameters4.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters4.Add("@guid", Guid.NewGuid());

                sqlStrings.Add(updateString4.ToString());
                parameterList.Add(updateStringparameters4);
                #endregion

                oldStock[0].CartonNo = checkCartonNo(oldStock[0].CartonNo, splitCount);

                oldStock[0].Qty = splitNum;

                

                //if (packlist.Count != 0)
                //{
                //    int intRackNmu = Convert.ToInt32(packlist.FirstOrDefault().RackNum);
                //    int rackNum = intRackNmu + splitNum;
                //    //更新周转箱明细中的数量
                //    StringBuilder updateString2 = new StringBuilder(1024);
                //    updateString2.Append("UPDATE dbo.wms_rack_package SET rack_num = @rack_num ,update_userid = @update_userid ,update_time = @update_time WHERE ruid = @ruid ");
                //    DynamicParameters updateStringparameters9 = new DynamicParameters();
                //    updateStringparameters9.Add("@rack_num", rackNum);
                //    updateStringparameters9.Add("@carton_no", packlist.FirstOrDefault().Ruid);
                //    updateStringparameters9.Add("@update_userid", loginId);
                //    updateStringparameters9.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                //    sqlStrings.Add(updateString2.ToString());
                //    parameterList.Add(updateStringparameters9);
                //}
                //else
                //{
                    //插入 wms_rack_package 周转箱明细表
                    StringBuilder insterString2 = new StringBuilder(1024);
                    DynamicParameters updateStringparameters7 = new DynamicParameters();
                    insterString2.Append(@"INSERT INTO [dbo].[wms_rack_package]
                                 ([rack_no]
                                 ,[corton_no]
                                 ,[packageType]
                                 ,[part_no]
                                 ,[factory_id]
                                 ,[factory_code]
                                 ,[enabled]
                                 ,[create_userid]
                                 ,[create_time]
                                 ,[update_userid]
                                 ,[update_time]
                                 ,[guid],[rack_num])
                           VALUES (@rack_no,@corton_no,@pack_type,@part_no,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid,@rack_num)");
                    updateStringparameters7 = new DynamicParameters();
                    updateStringparameters7.Add("@rack_no", rackNo);
                    updateStringparameters7.Add("@corton_no", oldStock[0].CartonNo);
                    updateStringparameters7.Add("@pack_type", 0);
                    updateStringparameters7.Add("@part_no", oldStock[0].PartNo);
                    updateStringparameters7.Add("@factory_id", oldStock[0].FactoryId);
                    updateStringparameters7.Add("@factory_code", oldStock[0].FactoryCode);
                    updateStringparameters7.Add("@enabled", "Y");
                    updateStringparameters7.Add("@create_userid", loginId);
                    updateStringparameters7.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateStringparameters7.Add("@guid", Guid.NewGuid());
                    updateStringparameters7.Add("@rack_num", splitNum);
                    updateStringparameters7.Add("@update_userid", null);
                    updateStringparameters7.Add("@update_time", null);

                    sqlStrings.Add(insterString2.ToString());
                    parameterList.Add(updateStringparameters7);
                //}




                //插入拆分出的箱条码
                #region 插入拆分出的箱条码

                StringBuilder updateString3 = new StringBuilder(1024);
                updateString3.Append(@" INSERT INTO [dbo].[sys_stock]
                                        ([vendor_id]
                                        ,[vendor_code]
                                        ,[carton_no]
                                        ,[part_id]
                                        ,[part_no]
                                        ,[part_spec]
                                        ,[lot_no]
                                        ,[fix_lot_no]
                                        ,[status]
                                        ,[qty]
                                        ,[snp_qty]
                                        ,[locate_id]
                                        ,[locate_name]
                                        ,[group_no]
                                        ,[erp_warehouse]
                                        ,[date_code]
                                        ,[qms_status]
                                        ,[ref_order_no]
                                        ,[unit]
                                        ,[dock]
                                        ,[warehouse_id]
                                        ,[warehouse_name]
                                        ,[zone_id]
                                        ,[zone_name]
                                        ,[printed]
                                        ,[print_time]
                                        ,[remark]
                                        ,[factory_id]
                                        ,[factory_code]
                                        ,[enabled]
                                        ,[create_userid]
                                        ,[create_time]
                                        ,[update_userid]
                                        ,[update_time]
                                        ,[guid])
                                        VALUES(@vendor_id,@vendor_code,@carton_no,@part_id,@part_no,@part_spec,@lot_no,@fix_lot_no,@status,@qty,@snp_qty
,@locate_id,@locate_name,@group_no,@erp_warehouse,@date_code,@qms_status,@ref_order_no,@unit,@dock,@warehouse_id,@warehouse_name,@zone_id,@zone_name
,@printed,@print_time,@remark,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid )");

                DynamicParameters updateStringparameters3 = new DynamicParameters();
                updateStringparameters3.Add("@vendor_id", oldStock[0].VendorId);
                updateStringparameters3.Add("@vendor_code", oldStock[0].VendorName);
                updateStringparameters3.Add("@carton_no", oldStock[0].CartonNo);
                updateStringparameters3.Add("@part_id", oldStock[0].PartId);
                updateStringparameters3.Add("@part_no", oldStock[0].PartNo);
                updateStringparameters3.Add("@part_spec", oldStock[0].PartSpec);
                updateStringparameters3.Add("@lot_no", oldStock[0].LotNo);
                updateStringparameters3.Add("@fix_lot_no", oldStock[0].FixLotNo);
                updateStringparameters3.Add("@status", oldStock[0].Status); //原状态
                updateStringparameters3.Add("@qty", splitNum);
                updateStringparameters3.Add("@snp_qty", oldStock[0].SnpQty);
                updateStringparameters3.Add("@locate_id", locateId);
                updateStringparameters3.Add("@locate_name", locateCode);  //默认在途
                updateStringparameters3.Add("@group_no", oldStock[0].GroupNo);
                updateStringparameters3.Add("@erp_warehouse", oldStock[0].ErpWarehouse);
                updateStringparameters3.Add("@date_code", oldStock[0].DateCode);
                updateStringparameters3.Add("@qms_status", oldStock[0].QmsStatus);
                updateStringparameters3.Add("@ref_order_no", oldStock[0].RefOrderNo);
                updateStringparameters3.Add("@unit", oldStock[0].Unit);
                updateStringparameters3.Add("@dock", oldStock[0].Dock);
                updateStringparameters3.Add("@warehouse_id", oldStock[0].WarehouseId);
                updateStringparameters3.Add("@warehouse_name", oldStock[0].WarehouseName);
                updateStringparameters3.Add("@zone_id", oldStock[0].ZoneId);
                updateStringparameters3.Add("@zone_name", oldStock[0].ZoneName);
                updateStringparameters3.Add("@printed", oldStock[0].Printed);
                updateStringparameters3.Add("@print_time", oldStock[0].PrintTime);
                updateStringparameters3.Add("@remark", oldStock[0].Remark);
                updateStringparameters3.Add("@factory_id", oldStock[0].FactoryId);
                updateStringparameters3.Add("@factory_code", oldStock[0].FactoryCode);
                updateStringparameters3.Add("@enabled", oldStock[0].Enabled);
                updateStringparameters3.Add("@update_userid", loginId);
                updateStringparameters3.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters3.Add("@create_userid", oldStock[0].CreateUserId);
                updateStringparameters3.Add("@create_time", oldStock[0].CreateTime);
                updateStringparameters3.Add("@guid", Guid.NewGuid());

                sqlStrings.Add(updateString3.ToString());
                parameterList.Add(updateStringparameters3);
                #endregion

                //新增 sys_stock_trans 事务表
                #region sys_stock_trans 事务表

                StringBuilder updateString5 = new StringBuilder(1024);
                updateString5.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                DynamicParameters updateStringparameters5 = new DynamicParameters();
                updateStringparameters5.Add("@trans_code", (int)WmsEnumUtil.TransType.PART_SPLIT);
                updateStringparameters5.Add("@carton_no", oldStock[0].CartonNo);
                updateStringparameters5.Add("@part_id", oldStock[0].PartId);
                updateStringparameters5.Add("@part_no", oldStock[0].PartNo);
                updateStringparameters5.Add("@part_spec", oldStock[0].PartSpec);
                updateStringparameters5.Add("@src_locate_id", oldStock[0].LocateId);
                updateStringparameters5.Add("@src_locate_name", oldStock[0].LocateName);
                updateStringparameters5.Add("@dest_locate_id", oldStock[0].LocateId);
                updateStringparameters5.Add("@dest_locate_name", oldStock[0].LocateName);
                updateStringparameters5.Add("@old_qty", 0);
                updateStringparameters5.Add("@new_qty", splitNum);
                updateStringparameters5.Add("@trans_qty", oldStock[0].Qty);
                updateStringparameters5.Add("@old_status", oldStock[0].Status);
                updateStringparameters5.Add("@new_status", oldStock[0].Status);
                updateStringparameters5.Add("@old_qms_status", oldStock[0].QmsStatus);
                updateStringparameters5.Add("@new_qms_status", oldStock[0].QmsStatus);
                updateStringparameters5.Add("@unit", oldStock[0].Unit);
                updateStringparameters5.Add("@factory_id", oldStock[0].FactoryId);
                updateStringparameters5.Add("@factory_code", oldStock[0].FactoryCode);
                updateStringparameters5.Add("@src_erp_warehouse", oldStock[0].ErpWarehouse);
                updateStringparameters5.Add("@dest_erp_warehouse", oldStock[0].ErpWarehouse);
                updateStringparameters5.Add("@src_warehouse_id", oldStock[0].WarehouseId);
                updateStringparameters5.Add("@src_warehouse_name", oldStock[0].WarehouseName);
                updateStringparameters5.Add("@dest_warehouse_id", oldStock[0].WarehouseId);
                updateStringparameters5.Add("@dest_warehouse_name", oldStock[0].WarehouseName);
                updateStringparameters5.Add("@src_zone_id", oldStock[0].ZoneId);
                updateStringparameters5.Add("@src_zone_name", oldStock[0].ZoneName);
                updateStringparameters5.Add("@dest_zone_id", oldStock[0].ZoneId);
                updateStringparameters5.Add("@dest_zone_name", oldStock[0].ZoneName);
                updateStringparameters5.Add("@enabled", oldStock[0].Enabled);
                updateStringparameters5.Add("@update_userid", loginId);
                updateStringparameters5.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters5.Add("@create_userid", loginId);
                updateStringparameters5.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters5.Add("@guid", Guid.NewGuid());

                sqlStrings.Add(updateString5.ToString());
                parameterList.Add(updateStringparameters5);
                #endregion


                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();
                    return oldStock;
                }
                else
                {
                    transaction.Rollback();
                    return null;
                }
            }
        }







        /// <summary>
        /// 移库单配料-拆分
        /// </summary>
        /// <param name="oldStock"></param>
        /// <param name="splitNum"></param>
        /// <param name="loginId"></param>
        /// <param name="splitCount"></param>
        /// <returns></returns>
        public List<SysStock> CheckMaterialSplit(int locateId, string locateCode, string rackNo, List<WmsRackPackage_Extend> packlist, List<WmsMoveDetail_Extend> detInfo, List<SysStock> oldStock, int splitNum, string loginId, int splitCount)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                decimal oldQty = oldStock[0].Qty;
                decimal newQty = oldStock[0].Qty - splitNum;

                //更新条码表，数量
                StringBuilder updateString = new StringBuilder(1024);
                updateString.Append("update sys_stock set qty=@qty  where carton_no=@carton_no ");
                DynamicParameters updateStringparameters = new DynamicParameters();
                updateStringparameters.Add("@qty", newQty);
                updateStringparameters.Add("@carton_no", oldStock[0].CartonNo);
                updateStringparameters.Add("@update_userid", loginId);
                updateStringparameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                sqlStrings.Add(updateString.ToString());
                parameterList.Add(updateStringparameters);


                //新增 sys_stock_trans 事务表
                #region sys_stock_trans 事务表

                StringBuilder updateString4 = new StringBuilder(1024);
                updateString4.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                DynamicParameters updateStringparameters4 = new DynamicParameters();
                updateStringparameters4.Add("@trans_code", (int)WmsEnumUtil.TransType.PART_SPLIT);
                updateStringparameters4.Add("@carton_no", oldStock[0].CartonNo);
                updateStringparameters4.Add("@part_id", oldStock[0].PartId);
                updateStringparameters4.Add("@part_no", oldStock[0].PartNo);
                updateStringparameters4.Add("@part_spec", oldStock[0].PartSpec);
                updateStringparameters4.Add("@src_locate_id", oldStock[0].LocateId);
                updateStringparameters4.Add("@src_locate_name", oldStock[0].LocateName);
                updateStringparameters4.Add("@dest_locate_id", oldStock[0].LocateId);
                updateStringparameters4.Add("@dest_locate_name", oldStock[0].LocateName);
                updateStringparameters4.Add("@old_qty", oldQty);
                updateStringparameters4.Add("@new_qty", newQty);
                updateStringparameters4.Add("@trans_qty", oldStock[0].Qty);
                updateStringparameters4.Add("@old_status", oldStock[0].Status);
                updateStringparameters4.Add("@new_status", oldStock[0].Status);
                updateStringparameters4.Add("@old_qms_status", oldStock[0].QmsStatus);
                updateStringparameters4.Add("@new_qms_status", oldStock[0].QmsStatus);
                updateStringparameters4.Add("@unit", oldStock[0].Unit);
                updateStringparameters4.Add("@factory_id", oldStock[0].FactoryId);
                updateStringparameters4.Add("@factory_code", oldStock[0].FactoryCode);
                updateStringparameters4.Add("@src_erp_warehouse", oldStock[0].ErpWarehouse);
                updateStringparameters4.Add("@dest_erp_warehouse", oldStock[0].ErpWarehouse);
                updateStringparameters4.Add("@src_warehouse_id", oldStock[0].WarehouseId);
                updateStringparameters4.Add("@src_warehouse_name", oldStock[0].WarehouseName);
                updateStringparameters4.Add("@dest_warehouse_id", oldStock[0].WarehouseId);
                updateStringparameters4.Add("@dest_warehouse_name", oldStock[0].WarehouseName);
                updateStringparameters4.Add("@src_zone_id", oldStock[0].ZoneId);
                updateStringparameters4.Add("@src_zone_name", oldStock[0].ZoneName);
                updateStringparameters4.Add("@dest_zone_id", oldStock[0].ZoneId);
                updateStringparameters4.Add("@dest_zone_name", oldStock[0].ZoneName);
                updateStringparameters4.Add("@enabled", oldStock[0].Enabled);
                updateStringparameters4.Add("@update_userid", loginId);
                updateStringparameters4.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters4.Add("@create_userid", loginId);
                updateStringparameters4.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters4.Add("@guid", Guid.NewGuid());

                sqlStrings.Add(updateString4.ToString());
                parameterList.Add(updateStringparameters4);
                #endregion

                oldStock[0].CartonNo = checkCartonNo(oldStock[0].CartonNo, splitCount);

                oldStock[0].Qty = splitNum;


                //插入 wms_move_sn 条码表
                StringBuilder updateString8 = new StringBuilder(1024);
                updateString8.Append(@"INSERT INTO [dbo].[wms_move_sn]
                                        ([order_no]
                                        ,[item_no]
                                        ,[part_id]
                                        ,[part_no]
                                        ,[part_spec]
                                        ,[carton_no]
                                        ,[qty]
                                        ,[unit]
                                        ,[status]
                                        ,[factory_id]
                                        ,[factory_code]
                                        ,[enabled]
                                        ,[create_userid]
                                        ,[create_time]
                                        ,[update_userid]
                                        ,[update_time]
                                        ,[guid])
                                         VALUES (@order_no,@item_no,@part_id,@part_no,@part_spec,@carton_no,@qty,@unit,@status,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid)");
                DynamicParameters updateStringparameters8 = new DynamicParameters();
                updateStringparameters8.Add("@order_no", detInfo[0].OrderNo);
                updateStringparameters8.Add("@item_no", detInfo[0].ItemNo);
                updateStringparameters8.Add("@part_id", detInfo[0].PartId);
                updateStringparameters8.Add("@part_no", detInfo[0].PartNo);
                updateStringparameters8.Add("@part_spec", detInfo[0].PartSpec);
                updateStringparameters8.Add("@carton_no", oldStock[0].CartonNo);
                updateStringparameters8.Add("@qty", splitNum);
                updateStringparameters8.Add("@unit", oldStock[0].Unit);
                updateStringparameters8.Add("@status", (int)WmsEnumUtil.MoveOrderSnStatus.BATCHED);
                updateStringparameters8.Add("@factory_id", detInfo[0].FactoryId);
                updateStringparameters8.Add("@factory_code", detInfo[0].FactoryCode);
                updateStringparameters8.Add("@enabled", "Y");
                updateStringparameters8.Add("@update_userid", loginId);
                updateStringparameters8.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters8.Add("@create_userid", loginId);
                updateStringparameters8.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters8.Add("@guid", Guid.NewGuid());

                sqlStrings.Add(updateString8.ToString());
                parameterList.Add(updateStringparameters8);

                if (packlist.Count != 0)
                {
                    int intRackNmu = Convert.ToInt32(packlist.FirstOrDefault().RackNum);
                    int rackNum = intRackNmu + splitNum;
                    //更新周转箱明细中的数量
                    StringBuilder updateString2 = new StringBuilder(1024);
                    updateString2.Append("UPDATE dbo.wms_rack_package SET rack_num = @rack_num ,update_userid = @update_userid ,update_time = @update_time WHERE ruid = @ruid ");
                    DynamicParameters updateStringparameters9 = new DynamicParameters();
                    updateStringparameters9.Add("@rack_num", rackNum);
                    updateStringparameters9.Add("@carton_no", packlist.FirstOrDefault().Ruid);
                    updateStringparameters9.Add("@update_userid", loginId);
                    updateStringparameters9.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    sqlStrings.Add(updateString2.ToString());
                    parameterList.Add(updateStringparameters9);
                }
                else
                {
                    //插入 wms_rack_package 周转箱明细表
                    StringBuilder insterString2 = new StringBuilder(1024);
                    DynamicParameters updateStringparameters7 = new DynamicParameters();
                    insterString2.Append(@"INSERT INTO [dbo].[wms_rack_package]
                                 ([rack_no]
                                 ,[corton_no]
                                 ,[packageType]
                                 ,[part_no]
                                 ,[factory_id]
                                 ,[factory_code]
                                 ,[enabled]
                                 ,[create_userid]
                                 ,[create_time]
                                 ,[update_userid]
                                 ,[update_time]
                                 ,[guid],[rack_num])
                           VALUES (@rack_no,@corton_no,@pack_type,@part_no,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid,@rack_num)");
                    updateStringparameters7 = new DynamicParameters();
                    updateStringparameters7.Add("@rack_no", rackNo);
                    updateStringparameters7.Add("@corton_no", oldStock[0].CartonNo);
                    updateStringparameters7.Add("@pack_type", 0);
                    updateStringparameters7.Add("@part_no", detInfo[0].PartNo);
                    updateStringparameters7.Add("@factory_id", detInfo[0].FactoryId);
                    updateStringparameters7.Add("@factory_code", detInfo[0].FactoryCode);
                    updateStringparameters7.Add("@enabled", "Y");
                    updateStringparameters7.Add("@create_userid", loginId);
                    updateStringparameters7.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateStringparameters7.Add("@guid", Guid.NewGuid());
                    updateStringparameters7.Add("@rack_num", splitNum);
                    updateStringparameters7.Add("@update_userid", null);
                    updateStringparameters7.Add("@update_time", null);

                    sqlStrings.Add(insterString2.ToString());
                    parameterList.Add(updateStringparameters7);
                }




                //插入拆分出的箱条码
                #region 插入拆分出的箱条码

                StringBuilder updateString3 = new StringBuilder(1024);
                updateString3.Append(@" INSERT INTO [dbo].[sys_stock]
                                        ([vendor_id]
                                        ,[vendor_code]
                                        ,[carton_no]
                                        ,[part_id]
                                        ,[part_no]
                                        ,[part_spec]
                                        ,[lot_no]
                                        ,[fix_lot_no]
                                        ,[status]
                                        ,[qty]
                                        ,[snp_qty]
                                        ,[locate_id]
                                        ,[locate_name]
                                        ,[group_no]
                                        ,[erp_warehouse]
                                        ,[date_code]
                                        ,[qms_status]
                                        ,[ref_order_no]
                                        ,[unit]
                                        ,[dock]
                                        ,[warehouse_id]
                                        ,[warehouse_name]
                                        ,[zone_id]
                                        ,[zone_name]
                                        ,[printed]
                                        ,[print_time]
                                        ,[remark]
                                        ,[factory_id]
                                        ,[factory_code]
                                        ,[enabled]
                                        ,[create_userid]
                                        ,[create_time]
                                        ,[update_userid]
                                        ,[update_time]
                                        ,[guid])
                                        VALUES(@vendor_id,@vendor_code,@carton_no,@part_id,@part_no,@part_spec,@lot_no,@fix_lot_no,@status,@qty,@snp_qty
,@locate_id,@locate_name,@group_no,@erp_warehouse,@date_code,@qms_status,@ref_order_no,@unit,@dock,@warehouse_id,@warehouse_name,@zone_id,@zone_name
,@printed,@print_time,@remark,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid )");

                DynamicParameters updateStringparameters3 = new DynamicParameters();
                updateStringparameters3.Add("@vendor_id", oldStock[0].VendorId);
                updateStringparameters3.Add("@vendor_code", oldStock[0].VendorName);
                updateStringparameters3.Add("@carton_no", oldStock[0].CartonNo);
                updateStringparameters3.Add("@part_id", oldStock[0].PartId);
                updateStringparameters3.Add("@part_no", oldStock[0].PartNo);
                updateStringparameters3.Add("@part_spec", oldStock[0].PartSpec);
                updateStringparameters3.Add("@lot_no", oldStock[0].LotNo);
                updateStringparameters3.Add("@fix_lot_no", oldStock[0].FixLotNo);
                updateStringparameters3.Add("@status", 60); //原状态
                updateStringparameters3.Add("@qty", splitNum);
                updateStringparameters3.Add("@snp_qty", oldStock[0].SnpQty);
                updateStringparameters3.Add("@locate_id", locateId);
                updateStringparameters3.Add("@locate_name", locateCode);  //默认在途
                updateStringparameters3.Add("@group_no", oldStock[0].GroupNo);
                updateStringparameters3.Add("@erp_warehouse", oldStock[0].ErpWarehouse);
                updateStringparameters3.Add("@date_code", oldStock[0].DateCode);
                updateStringparameters3.Add("@qms_status", oldStock[0].QmsStatus);
                updateStringparameters3.Add("@ref_order_no", oldStock[0].RefOrderNo);
                updateStringparameters3.Add("@unit", oldStock[0].Unit);
                updateStringparameters3.Add("@dock", oldStock[0].Dock);
                updateStringparameters3.Add("@warehouse_id", oldStock[0].WarehouseId);
                updateStringparameters3.Add("@warehouse_name", oldStock[0].WarehouseName);
                updateStringparameters3.Add("@zone_id", oldStock[0].ZoneId);
                updateStringparameters3.Add("@zone_name", oldStock[0].ZoneName);
                updateStringparameters3.Add("@printed", oldStock[0].Printed);
                updateStringparameters3.Add("@print_time", oldStock[0].PrintTime);
                updateStringparameters3.Add("@remark", oldStock[0].Remark);
                updateStringparameters3.Add("@factory_id", oldStock[0].FactoryId);
                updateStringparameters3.Add("@factory_code", oldStock[0].FactoryCode);
                updateStringparameters3.Add("@enabled", oldStock[0].Enabled);
                updateStringparameters3.Add("@update_userid", loginId);
                updateStringparameters3.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters3.Add("@create_userid", oldStock[0].CreateUserId);
                updateStringparameters3.Add("@create_time", oldStock[0].CreateTime);
                updateStringparameters3.Add("@guid", Guid.NewGuid());

                sqlStrings.Add(updateString3.ToString());
                parameterList.Add(updateStringparameters3);
                #endregion

                //新增 sys_stock_trans 事务表
                #region sys_stock_trans 事务表

                StringBuilder updateString5 = new StringBuilder(1024);
                updateString5.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                DynamicParameters updateStringparameters5 = new DynamicParameters();
                updateStringparameters5.Add("@trans_code", (int)WmsEnumUtil.TransType.PART_SPLIT);
                updateStringparameters5.Add("@carton_no", oldStock[0].CartonNo);
                updateStringparameters5.Add("@part_id", oldStock[0].PartId);
                updateStringparameters5.Add("@part_no", oldStock[0].PartNo);
                updateStringparameters5.Add("@part_spec", oldStock[0].PartSpec);
                updateStringparameters5.Add("@src_locate_id", oldStock[0].LocateId);
                updateStringparameters5.Add("@src_locate_name", oldStock[0].LocateName);
                updateStringparameters5.Add("@dest_locate_id", oldStock[0].LocateId);
                updateStringparameters5.Add("@dest_locate_name", oldStock[0].LocateName);
                updateStringparameters5.Add("@old_qty", 0);
                updateStringparameters5.Add("@new_qty", splitNum);
                updateStringparameters5.Add("@trans_qty", oldStock[0].Qty);
                updateStringparameters5.Add("@old_status", oldStock[0].Status);
                updateStringparameters5.Add("@new_status", oldStock[0].Status);
                updateStringparameters5.Add("@old_qms_status", oldStock[0].QmsStatus);
                updateStringparameters5.Add("@new_qms_status", oldStock[0].QmsStatus);
                updateStringparameters5.Add("@unit", oldStock[0].Unit);
                updateStringparameters5.Add("@factory_id", oldStock[0].FactoryId);
                updateStringparameters5.Add("@factory_code", oldStock[0].FactoryCode);
                updateStringparameters5.Add("@src_erp_warehouse", oldStock[0].ErpWarehouse);
                updateStringparameters5.Add("@dest_erp_warehouse", oldStock[0].ErpWarehouse);
                updateStringparameters5.Add("@src_warehouse_id", oldStock[0].WarehouseId);
                updateStringparameters5.Add("@src_warehouse_name", oldStock[0].WarehouseName);
                updateStringparameters5.Add("@dest_warehouse_id", oldStock[0].WarehouseId);
                updateStringparameters5.Add("@dest_warehouse_name", oldStock[0].WarehouseName);
                updateStringparameters5.Add("@src_zone_id", oldStock[0].ZoneId);
                updateStringparameters5.Add("@src_zone_name", oldStock[0].ZoneName);
                updateStringparameters5.Add("@dest_zone_id", oldStock[0].ZoneId);
                updateStringparameters5.Add("@dest_zone_name", oldStock[0].ZoneName);
                updateStringparameters5.Add("@enabled", oldStock[0].Enabled);
                updateStringparameters5.Add("@update_userid", loginId);
                updateStringparameters5.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters5.Add("@create_userid", loginId);
                updateStringparameters5.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters5.Add("@guid", Guid.NewGuid());

                sqlStrings.Add(updateString5.ToString());
                parameterList.Add(updateStringparameters5);
                #endregion


                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false; 
                    }
                }
                if (successCount)
                {
                    transaction.Commit();
                    return oldStock;
                }
                else
                {
                    transaction.Rollback();
                    return null;
                }
            }
        }

        public string checkCartonNo(string carton, int splitCount)
        {
            List<SysStock> MaterialInfo = GetMaterialInfo(carton + "-" + splitCount);
            if (MaterialInfo.Count > 0)
            {
                //有重复
                return checkCartonNo(carton, splitCount + 1);
            }
            else
            {
                return carton + "-" + splitCount;
            }
        }

        public List<SysStock> GetMaterialInfo(string cartonNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("select * from sys_stock (nolock) where carton_no= @carton_no ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@carton_no", cartonNo);

                return dbConn.Query<SysStock>(sql.ToString(), parameters).ToList();
            }
        }


        public SetObjectDetail UpdteMoveInfo(List<WmsMoveDetail_Extend> wmsMoveDetails, SysStock Stock, string loginId, string SplitQty,int locateId, string locateName)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                SetObjectDetail rfInfo = new SetObjectDetail();//返回消息
                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                int intSplitQty = Convert.ToInt32(SplitQty);

                if (wmsMoveDetails.Count > 0)
                {
                    for (int i = 0; i < wmsMoveDetails.Count; i++)
                    {
                        StringBuilder updateString2 = new StringBuilder(1024);
                        updateString2.Append(@"update wms_move_detail set dest_locate_id=@dest_locate_id,dest_zone_name=@dest_zone_name,pick_qty = pick_qty + @pick_qty,out_qty=out_qty+@out_qty, item_status=@item_status,update_userid=@update_userid,update_time=@update_time where ruid=@ruid ");
                        DynamicParameters updateStringparameters2 = new DynamicParameters();
                        updateStringparameters2.Add("@item_status", (int)WmsEnumUtil.MoveOrderDetailStatus.BATCHING);
                        updateStringparameters2.Add("@ruid", wmsMoveDetails[i].Ruid);
                        updateStringparameters2.Add("@dest_locate_id", locateId);
                        updateStringparameters2.Add("@dest_zone_name", locateName);
                        updateStringparameters2.Add("@pick_qty", SplitQty == "" ? 0 : Convert.ToInt32(SplitQty));
                        updateStringparameters2.Add("@out_qty", SplitQty == "" ? 0 : Convert.ToInt32(SplitQty));
                        updateStringparameters2.Add("@update_userid", loginId);
                        updateStringparameters2.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                        sqlStrings.Add(updateString2.ToString());
                        parameterList.Add(updateStringparameters2);



                        //插入 wms_move_sn 条码表
                        StringBuilder updateString3 = new StringBuilder(1024);
                        updateString3.Append(@"INSERT INTO [dbo].[wms_move_sn]
                                    ([order_no]
                                    ,[item_no]
                                    ,[part_id]
                                    ,[part_no]
                                    ,[part_spec]
                                    ,[carton_no]
                                    ,[qty]
                                    ,[unit]
                                    ,[status]
                                    ,[factory_id]
                                    ,[factory_code]
                                    ,[enabled]
                                    ,[create_userid]
                                    ,[create_time]
                                    ,[update_userid]
                                    ,[update_time]
                                    ,[guid])
                                     VALUES (@order_no,@item_no,@part_id,@part_no,@part_spec,@carton_no,@qty,@unit,@status,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid)");
                        DynamicParameters updateStringparameters3 = new DynamicParameters();
                        updateStringparameters3.Add("@order_no", wmsMoveDetails[i].OrderNo);
                        updateStringparameters3.Add("@item_no", wmsMoveDetails[i].ItemNo);
                        updateStringparameters3.Add("@part_id", wmsMoveDetails[i].PartId);
                        updateStringparameters3.Add("@part_no", wmsMoveDetails[i].PartNo);
                        updateStringparameters3.Add("@part_spec", wmsMoveDetails[i].PartSpec);
                        updateStringparameters3.Add("@carton_no", Stock.CartonNo);
                        updateStringparameters3.Add("@qty", SplitQty);
                        updateStringparameters3.Add("@unit", Stock.Unit);
                        updateStringparameters3.Add("@status", (int)WmsEnumUtil.MoveOrderSnStatus.BATCHED);
                        updateStringparameters3.Add("@factory_id", wmsMoveDetails[i].FactoryId);
                        updateStringparameters3.Add("@factory_code", wmsMoveDetails[i].FactoryCode);
                        updateStringparameters3.Add("@enabled", "Y");
                        updateStringparameters3.Add("@update_userid", loginId);
                        updateStringparameters3.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        updateStringparameters3.Add("@create_userid", loginId);
                        updateStringparameters3.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        updateStringparameters3.Add("@guid", Guid.NewGuid());

                        sqlStrings.Add(updateString3.ToString());
                        parameterList.Add(updateStringparameters3);
                    }
                }


               

                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();
                    rfInfo.type = "PASS";
                    rfInfo.message = "[" + Stock.CartonNo + "]:单据操作完成!";
                }
                return rfInfo;
            }
        }


        public SetObjectDetail UpdtePickingInfo(WmsRackPackage_Extend pack, SysStock Stock, string loginId, string rackNo, string SplitQty, string rackOrCartNo, int? locateId, string locateName)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                SetObjectDetail rfInfo = new SetObjectDetail();//返回消息
                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                int intSplitQty = Convert.ToInt32(SplitQty);
                

                if (pack != null)
                {
                    int intRackNum = Convert.ToInt32(pack.RackNum);
                    if (intRackNum != intSplitQty)
                    {
                        //int newRackNum = intRackNum - intSplitQty;
                        //周转箱中扫描的物料进行解绑
                        StringBuilder updateString2 = new StringBuilder(1024);
                        updateString2.Append(@"UPDATE dbo.wms_rack_package SET rack_num=@rack_num,update_userid=@update_userid,update_time=@update_time WHERE ruid=@ruid AND enabled='Y'  ");
                        DynamicParameters updateStringparameters2 = new DynamicParameters();
                        updateStringparameters2.Add("@ruid", pack.Ruid);
                        updateStringparameters2.Add("@rack_num", Stock.Qty);
                        updateStringparameters2.Add("@update_userid", loginId);
                        updateStringparameters2.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                        sqlStrings.Add(updateString2.ToString());
                        parameterList.Add(updateStringparameters2);
                    }
                }

                
                //新增 sys_stock_trans 事务表
                StringBuilder updateString4 = new StringBuilder(1024);
                updateString4.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                DynamicParameters updateStringparameters4 = new DynamicParameters();
                updateStringparameters4.Add("@trans_code", (int)WmsEnumUtil.TransType.OUTWARD_PICK);
                updateStringparameters4.Add("@carton_no", Stock.CartonNo);
                updateStringparameters4.Add("@part_id", Stock.PartId);
                updateStringparameters4.Add("@part_no", Stock.PartNo);
                updateStringparameters4.Add("@part_spec", Stock.PartSpec);
                updateStringparameters4.Add("@src_locate_id", Stock.LocateId);
                updateStringparameters4.Add("@src_locate_name", Stock.LocateName);
                updateStringparameters4.Add("@dest_locate_id", locateId);
                updateStringparameters4.Add("@dest_locate_name", locateName);
                updateStringparameters4.Add("@old_qty", Stock.Qty);
                updateStringparameters4.Add("@new_qty", Stock.Qty);
                updateStringparameters4.Add("@trans_qty", Stock.Qty);
                updateStringparameters4.Add("@old_status", Stock.Status);
                updateStringparameters4.Add("@new_status", (int)WmsEnumUtil.StockStatus.BATCHED);
                updateStringparameters4.Add("@old_qms_status", Stock.QmsStatus);
                updateStringparameters4.Add("@new_qms_status", Stock.QmsStatus);
                updateStringparameters4.Add("@unit", Stock.Unit);
                updateStringparameters4.Add("@factory_id", Stock.FactoryId);
                updateStringparameters4.Add("@factory_code", Stock.FactoryCode);
                updateStringparameters4.Add("@src_erp_warehouse", Stock.ErpWarehouse);
                updateStringparameters4.Add("@dest_erp_warehouse", Stock.ErpWarehouse);
                updateStringparameters4.Add("@src_warehouse_id", Stock.WarehouseId);
                updateStringparameters4.Add("@src_warehouse_name", Stock.WarehouseName);
                updateStringparameters4.Add("@dest_warehouse_id", Stock.WarehouseId);
                updateStringparameters4.Add("@dest_warehouse_name", Stock.WarehouseName);
                updateStringparameters4.Add("@src_zone_id", Stock.ZoneId);
                updateStringparameters4.Add("@src_zone_name", Stock.ZoneName);
                updateStringparameters4.Add("@dest_zone_id", Stock.ZoneId);
                updateStringparameters4.Add("@dest_zone_name", Stock.ZoneName);
                updateStringparameters4.Add("@enabled", Stock.Enabled);
                updateStringparameters4.Add("@update_userid", loginId);
                updateStringparameters4.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters4.Add("@create_userid", loginId);
                updateStringparameters4.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters4.Add("@guid", Guid.NewGuid());

                sqlStrings.Add(updateString4.ToString());
                parameterList.Add(updateStringparameters4);

                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();
                    rfInfo.type = "PASS";
                    rfInfo.message = "[" + Stock.CartonNo + "]:单据操作完成!";
                }
                return rfInfo;
            }
        }



        public SetObjectDetail UpdteStatusBatach(int? locateId, string locateName, string orderNo, List<WmsMoveDetail_Extend> detail, SysStock Stock, string loginId, string isSplit, string rackNo, string SplitQty, string rackOrCartNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                SetObjectDetail rfInfo = new SetObjectDetail();//返回消息
                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                for (int i = 0; i < detail.Count; i++)
                {
                    //修改移库主表  30 处理中
                    StringBuilder updateString = new StringBuilder(1024);
                    updateString.Append(" update wms_move_header set order_status=@order_status,update_userid=@update_userid,update_time=@update_time where order_no=@order_no ");
                    DynamicParameters updateStringparameters = new DynamicParameters();
                    updateStringparameters.Add("@order_status", (int)WmsEnumUtil.MoveOrderStatus.PROCESSING);
                    updateStringparameters.Add("@order_no", detail[i].OrderNo);
                    updateStringparameters.Add("@update_userid", loginId);
                    updateStringparameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    sqlStrings.Add(updateString.ToString());
                    parameterList.Add(updateStringparameters);

                    //修改移库明细表  30 处理中
                    StringBuilder updateString2 = new StringBuilder(1024);
                    updateString2.Append(@"update wms_move_detail set pick_qty = pick_qty + @pick_qty,out_qty=out_qty+@out_qty, item_status=@item_status,update_userid=@update_userid,update_time=@update_time where ruid=@ruid ");
                    DynamicParameters updateStringparameters2 = new DynamicParameters();
                    updateStringparameters2.Add("@item_status", (int)WmsEnumUtil.MoveOrderDetailStatus.BATCHING);
                    updateStringparameters2.Add("@ruid", detail[i].Ruid);
                    updateStringparameters2.Add("@pick_qty", SplitQty == "" ? 0 : Convert.ToInt32(SplitQty));
                    updateStringparameters2.Add("@out_qty", SplitQty == "" ? 0 : Convert.ToInt32(SplitQty));
                    updateStringparameters2.Add("@update_userid", loginId);
                    updateStringparameters2.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    sqlStrings.Add(updateString2.ToString());
                    parameterList.Add(updateStringparameters2);

                    //修改条码表  周转箱为在途
                    StringBuilder updateString5 = new StringBuilder(1024);
                    updateString5.Append("update sys_stock set relevance_order=@relevance_order,locate_id=@locate_id,locate_name=@locate_name,update_userid=@update_userid,update_time=@update_time  where carton_no= @carton_no ");
                    DynamicParameters updateStringparameters5 = new DynamicParameters();
                    updateStringparameters5.Add("@locate_id", locateId);
                    updateStringparameters5.Add("@locate_name", locateName);
                    updateStringparameters5.Add("@carton_no", rackNo);
                    updateStringparameters5.Add("@relevance_order", orderNo);
                    updateStringparameters5.Add("@update_userid", loginId);
                    updateStringparameters5.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    sqlStrings.Add(updateString5.ToString());
                    parameterList.Add(updateStringparameters5);


                    //插入 wms_move_sn 条码表
                    StringBuilder updateString3 = new StringBuilder(1024);
                    updateString3.Append(@"INSERT INTO [dbo].[wms_move_sn]
                                        ([order_no]
                                        ,[item_no]
                                        ,[part_id]
                                        ,[part_no]
                                        ,[part_spec]
                                        ,[carton_no]
                                        ,[qty]
                                        ,[unit]
                                        ,[status]
                                        ,[factory_id]
                                        ,[factory_code]
                                        ,[enabled]
                                        ,[create_userid]
                                        ,[create_time]
                                        ,[update_userid]
                                        ,[update_time]
                                        ,[guid])
                                         VALUES (@order_no,@item_no,@part_id,@part_no,@part_spec,@carton_no,@qty,@unit,@status,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid)");
                    DynamicParameters updateStringparameters3 = new DynamicParameters();
                    updateStringparameters3.Add("@order_no", detail[i].OrderNo);
                    updateStringparameters3.Add("@item_no", detail[i].ItemNo);
                    updateStringparameters3.Add("@part_id", detail[i].PartId);
                    updateStringparameters3.Add("@part_no", detail[i].PartNo);
                    updateStringparameters3.Add("@part_spec", detail[i].PartSpec);
                    updateStringparameters3.Add("@carton_no", Stock.CartonNo);  
                    updateStringparameters3.Add("@qty", SplitQty);
                    updateStringparameters3.Add("@unit", Stock.Unit);
                    updateStringparameters3.Add("@status", (int)WmsEnumUtil.MoveOrderSnStatus.BATCHED);
                    updateStringparameters3.Add("@factory_id", detail[i].FactoryId);
                    updateStringparameters3.Add("@factory_code", detail[i].FactoryCode);
                    updateStringparameters3.Add("@enabled", "Y");
                    updateStringparameters3.Add("@update_userid", loginId);
                    updateStringparameters3.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateStringparameters3.Add("@create_userid", loginId);
                    updateStringparameters3.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateStringparameters3.Add("@guid", Guid.NewGuid());

                    sqlStrings.Add(updateString3.ToString());
                    parameterList.Add(updateStringparameters3);


                    if (isSplit == "True")
                    {
                        if (!string.IsNullOrEmpty(rackOrCartNo))
                        {
                            //修改周转箱维护表数量
                            StringBuilder updateString12 = new StringBuilder(1024);
                            updateString12.Append(" update wms_rack_package set rack_num=@rack_num,update_userid=@update_userid,update_time=@update_time where corton_no=@corton_no ");
                            DynamicParameters updateStringparameters12 = new DynamicParameters();
                            updateStringparameters12.Add("@rack_num", Stock.Qty); //
                            updateStringparameters12.Add("@corton_no", Stock.CartonNo);
                            updateStringparameters12.Add("@update_userid", loginId);
                            updateStringparameters12.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                            sqlStrings.Add(updateString12.ToString());
                            parameterList.Add(updateStringparameters12);
                        }

                        //StringBuilder insterString2 = new StringBuilder(1024);
                        //DynamicParameters updateStringparameters8 = new DynamicParameters();
                        //insterString2.Append(@"INSERT INTO [dbo].[wms_rack_package]
                        //                 ([rack_no]
                        //                 ,[corton_no]
                        //                 ,[packageType]
                        //                 ,[part_no]
                        //                 ,[factory_id]
                        //                 ,[factory_code]
                        //                 ,[enabled]
                        //                 ,[create_userid]
                        //                 ,[create_time]
                        //                 ,[update_userid]
                        //                 ,[update_time]
                        //                 ,[guid],[rack_num])
                        //           VALUES (@rack_no,@corton_no,@pack_type,@part_no,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid,@rack_num)");
                        //updateStringparameters8 = new DynamicParameters();
                        //updateStringparameters8.Add("@rack_no", rackNo);
                        //updateStringparameters8.Add("@corton_no", Stock.CartonNo);
                        //updateStringparameters8.Add("@pack_type", 0);
                        //updateStringparameters8.Add("@part_no", detail[i].PartNo);
                        //updateStringparameters8.Add("@factory_id", detail[i].FactoryId);
                        //updateStringparameters8.Add("@factory_code", detail[i].FactoryCode);
                        //updateStringparameters8.Add("@enabled", "Y");
                        //updateStringparameters8.Add("@create_userid", loginId);
                        //updateStringparameters8.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        //updateStringparameters8.Add("@guid", Guid.NewGuid());
                        //updateStringparameters8.Add("@rack_num", SplitQty);  //数量不对
                        //updateStringparameters8.Add("@update_userid", null);
                        //updateStringparameters8.Add("@update_time", null);

                        //sqlStrings.Add(insterString2.ToString());
                        //parameterList.Add(updateStringparameters8);
                    }




                    //新增 sys_stock_trans 事务表
                    StringBuilder updateString4 = new StringBuilder(1024);
                    updateString4.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                    DynamicParameters updateStringparameters4 = new DynamicParameters();
                    updateStringparameters4.Add("@trans_code", (int)WmsEnumUtil.TransType.OUTWARD_PICK);
                    updateStringparameters4.Add("@carton_no", Stock.CartonNo);
                    updateStringparameters4.Add("@part_id", Stock.PartId);
                    updateStringparameters4.Add("@part_no", Stock.PartNo);
                    updateStringparameters4.Add("@part_spec", Stock.PartSpec);
                    updateStringparameters4.Add("@src_locate_id", Stock.LocateId);
                    updateStringparameters4.Add("@src_locate_name", Stock.LocateName);
                    updateStringparameters4.Add("@dest_locate_id", Stock.LocateId);
                    updateStringparameters4.Add("@dest_locate_name", Stock.LocateName);
                    updateStringparameters4.Add("@old_qty", Stock.Qty);
                    updateStringparameters4.Add("@new_qty", Stock.Qty);
                    updateStringparameters4.Add("@trans_qty", Stock.Qty);
                    updateStringparameters4.Add("@old_status", Stock.Status);
                    updateStringparameters4.Add("@new_status", (int)WmsEnumUtil.StockStatus.BATCHED);
                    updateStringparameters4.Add("@old_qms_status", Stock.QmsStatus);
                    updateStringparameters4.Add("@new_qms_status", Stock.QmsStatus);
                    updateStringparameters4.Add("@unit", Stock.Unit);
                    updateStringparameters4.Add("@factory_id", Stock.FactoryId);
                    updateStringparameters4.Add("@factory_code", Stock.FactoryCode);
                    updateStringparameters4.Add("@src_erp_warehouse", Stock.ErpWarehouse);
                    updateStringparameters4.Add("@dest_erp_warehouse", Stock.ErpWarehouse);
                    updateStringparameters4.Add("@src_warehouse_id", Stock.WarehouseId);
                    updateStringparameters4.Add("@src_warehouse_name", Stock.WarehouseName);
                    updateStringparameters4.Add("@dest_warehouse_id", Stock.WarehouseId);
                    updateStringparameters4.Add("@dest_warehouse_name", Stock.WarehouseName);
                    updateStringparameters4.Add("@src_zone_id", Stock.ZoneId);
                    updateStringparameters4.Add("@src_zone_name", Stock.ZoneName);
                    updateStringparameters4.Add("@dest_zone_id", Stock.ZoneId);
                    updateStringparameters4.Add("@dest_zone_name", Stock.ZoneName);
                    updateStringparameters4.Add("@enabled", Stock.Enabled);
                    updateStringparameters4.Add("@update_userid", loginId);
                    updateStringparameters4.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateStringparameters4.Add("@create_userid", loginId);
                    updateStringparameters4.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateStringparameters4.Add("@guid", Guid.NewGuid());

                    sqlStrings.Add(updateString4.ToString());
                    parameterList.Add(updateStringparameters4);
                }


                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();

                    StringBuilder stringBuilder = new StringBuilder(1024);
                    stringBuilder.Append("SELECT SUM( CONVERT(INT, c.qty)) qty,SUM(CONVERT(INT, c.pick_qty)) pick_qty,c.part_no,c.part_spec,(select top 1 locate_name from sys_stock (nolock) where part_id=c.part_id and status=" + (int)WmsEnumUtil.StockStatus.INSTOCKED + " order by create_time) as recommend_locate  FROM dbo.wms_move_header a (nolock) ");
                    stringBuilder.Append("LEFT JOIN dbo.wms_move_detail c (nolock) ON a.order_no=c.order_no ");
                    stringBuilder.Append(" WHERE a.order_no = '" + orderNo + "' AND order_status in ('30') AND order_type=" + (int)WmsEnumUtil.MoveOrderType.TRANSFER_ORDER + " and a.enabled='Y' group by c.part_id,c.part_no,c.part_spec");

                    List<WmsMoveDetail> wmsMoves1 = dbConn.Query<WmsMoveDetail>(stringBuilder.ToString()).ToList();
                    for (int i = 0; i < wmsMoves1.Count; i++)
                    {
                        if (wmsMoves1[i].Qty != wmsMoves1[i].PickQty)
                        {


                            rfInfo.type = "PASS";
                            rfInfo.message = "[" + Stock.CartonNo + "]:操作成功!请继续扫描箱条码或周转箱";
                            rfInfo.wmsObjectDetail = wmsMoves1.ConvertAll(s => (object)s);
                            return rfInfo;
                        }
                    }
                    rfInfo.type = "PASS";
                    rfInfo.message = "[" + Stock.CartonNo + "]:单据操作完成!";
                    rfInfo.wmsObjectDetail = null;
                    return rfInfo;

                }
                else
                {
                    transaction.Rollback();
                    rfInfo.type = "FAIL";
                    rfInfo.message = "未查询到数据，请检查！";
                    return rfInfo;
                }
            }
        }



        public SetObjectDetail UpdteStatus(int? locateId, string locateName, string orderNo, List<WmsMoveDetail_Extend> detail, SysStock Stock, string loginId, string isSplit, string rackNo, string SplitQty, string rackOrCartNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                SetObjectDetail rfInfo = new SetObjectDetail();//返回消息
                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                for (int i = 0; i < detail.Count; i++)
                {
                    //修改移库主表  30 处理中
                    StringBuilder updateString = new StringBuilder(1024);
                    updateString.Append(" update wms_move_header set order_status=@order_status,update_userid=@update_userid,update_time=@update_time where order_no=@order_no ");
                    DynamicParameters updateStringparameters = new DynamicParameters();
                    updateStringparameters.Add("@order_status", (int)WmsEnumUtil.MoveOrderStatus.PROCESSING);
                    updateStringparameters.Add("@order_no", detail[i].OrderNo);
                    updateStringparameters.Add("@update_userid", loginId);
                    updateStringparameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    sqlStrings.Add(updateString.ToString());
                    parameterList.Add(updateStringparameters);

                    //修改移库明细表  30 处理中
                    StringBuilder updateString2 = new StringBuilder(1024);
                    updateString2.Append(@"update wms_move_detail set pick_qty = pick_qty + @pick_qty,out_qty=out_qty+@out_qty, item_status=@item_status,update_userid=@update_userid,update_time=@update_time where ruid=@ruid ");
                    DynamicParameters updateStringparameters2 = new DynamicParameters();
                    updateStringparameters2.Add("@item_status", (int)WmsEnumUtil.MoveOrderDetailStatus.BATCHING);
                    updateStringparameters2.Add("@ruid", detail[i].Ruid);
                    updateStringparameters2.Add("@pick_qty", SplitQty==""?0:Convert.ToInt32(SplitQty));
                    updateStringparameters2.Add("@out_qty", SplitQty == "" ? 0 : Convert.ToInt32(SplitQty));
                    updateStringparameters2.Add("@update_userid", loginId);
                    updateStringparameters2.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    sqlStrings.Add(updateString2.ToString());
                    parameterList.Add(updateStringparameters2);

                    //修改条码表  周转箱为在途
                    StringBuilder updateString5 = new StringBuilder(1024);
                    updateString5.Append("update sys_stock set locate_id=@locate_id,locate_name=@locate_name,update_userid=@update_userid,update_time=@update_time  where carton_no= @carton_no ");
                    DynamicParameters updateStringparameters5 = new DynamicParameters();
                    updateStringparameters5.Add("@locate_id", locateId);
                    updateStringparameters5.Add("@locate_name", locateName);
                    updateStringparameters5.Add("@carton_no", rackNo);
                    //updateStringparameters5.Add("@status", (int)WmsEnumUtil.StockStatus.BATCHED);
                    updateStringparameters5.Add("@update_userid", loginId);
                    updateStringparameters5.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    sqlStrings.Add(updateString5.ToString());
                    parameterList.Add(updateStringparameters5);


                    ////插入 wms_move_sn 条码表
                    //StringBuilder updateString3 = new StringBuilder(1024);
                    //updateString3.Append(@"INSERT INTO [dbo].[wms_move_sn]
                    //                    ([order_no]
                    //                    ,[item_no]
                    //                    ,[part_id]
                    //                    ,[part_no]
                    //                    ,[part_spec]
                    //                    ,[carton_no]
                    //                    ,[qty]
                    //                    ,[unit]
                    //                    ,[status]
                    //                    ,[factory_id]
                    //                    ,[factory_code]
                    //                    ,[enabled]
                    //                    ,[create_userid]
                    //                    ,[create_time]
                    //                    ,[update_userid]
                    //                    ,[update_time]
                    //                    ,[guid])
                    //                     VALUES (@order_no,@item_no,@part_id,@part_no,@part_spec,@carton_no,@qty,@unit,@status,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid)");
                    //DynamicParameters updateStringparameters3 = new DynamicParameters();
                    //updateStringparameters3.Add("@order_no", detail[i].OrderNo);
                    //updateStringparameters3.Add("@item_no", detail[i].ItemNo);
                    //updateStringparameters3.Add("@part_id", detail[i].PartId);
                    //updateStringparameters3.Add("@part_no", detail[i].PartNo);
                    //updateStringparameters3.Add("@part_spec", detail[i].PartSpec);
                    //updateStringparameters3.Add("@carton_no", Stock.CartonNo);  //条码不对
                    //updateStringparameters3.Add("@qty", SplitQty); 
                    //updateStringparameters3.Add("@unit", Stock.Unit);
                    //updateStringparameters3.Add("@status", (int)WmsEnumUtil.MoveOrderSnStatus.BATCHED);
                    //updateStringparameters3.Add("@factory_id", detail[i].FactoryId);
                    //updateStringparameters3.Add("@factory_code", detail[i].FactoryCode);
                    //updateStringparameters3.Add("@enabled", "Y");
                    //updateStringparameters3.Add("@update_userid", loginId);
                    //updateStringparameters3.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    //updateStringparameters3.Add("@create_userid", loginId);
                    //updateStringparameters3.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    //updateStringparameters3.Add("@guid", Guid.NewGuid());

                    //sqlStrings.Add(updateString3.ToString());
                    //parameterList.Add(updateStringparameters3);


                    if (isSplit == "True")
                    {
                        if (!string.IsNullOrEmpty(rackOrCartNo))
                        {
                            //修改周转箱维护表数量
                            StringBuilder updateString12 = new StringBuilder(1024);
                            updateString12.Append(" update wms_rack_package set rack_num=@rack_num,update_userid=@update_userid,update_time=@update_time where corton_no=@corton_no ");
                            DynamicParameters updateStringparameters12 = new DynamicParameters();
                            updateStringparameters12.Add("@rack_num", Stock.Qty); //
                            updateStringparameters12.Add("@corton_no", Stock.CartonNo);
                            updateStringparameters12.Add("@update_userid", loginId);
                            updateStringparameters12.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                            sqlStrings.Add(updateString12.ToString());
                            parameterList.Add(updateStringparameters12);
                        }

                        //StringBuilder insterString2 = new StringBuilder(1024);
                        //DynamicParameters updateStringparameters8 = new DynamicParameters();
                        //insterString2.Append(@"INSERT INTO [dbo].[wms_rack_package]
                        //                 ([rack_no]
                        //                 ,[corton_no]
                        //                 ,[packageType]
                        //                 ,[part_no]
                        //                 ,[factory_id]
                        //                 ,[factory_code]
                        //                 ,[enabled]
                        //                 ,[create_userid]
                        //                 ,[create_time]
                        //                 ,[update_userid]
                        //                 ,[update_time]
                        //                 ,[guid],[rack_num])
                        //           VALUES (@rack_no,@corton_no,@pack_type,@part_no,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid,@rack_num)");
                        //updateStringparameters8 = new DynamicParameters();
                        //updateStringparameters8.Add("@rack_no", rackNo);
                        //updateStringparameters8.Add("@corton_no", Stock.CartonNo);
                        //updateStringparameters8.Add("@pack_type", 0);
                        //updateStringparameters8.Add("@part_no", detail[i].PartNo);
                        //updateStringparameters8.Add("@factory_id", detail[i].FactoryId);
                        //updateStringparameters8.Add("@factory_code", detail[i].FactoryCode);
                        //updateStringparameters8.Add("@enabled", "Y");
                        //updateStringparameters8.Add("@create_userid", loginId);
                        //updateStringparameters8.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        //updateStringparameters8.Add("@guid", Guid.NewGuid());
                        //updateStringparameters8.Add("@rack_num", SplitQty);  //数量不对
                        //updateStringparameters8.Add("@update_userid", null);
                        //updateStringparameters8.Add("@update_time", null);

                        //sqlStrings.Add(insterString2.ToString());
                        //parameterList.Add(updateStringparameters8);
                    }




                    //新增 sys_stock_trans 事务表
                    StringBuilder updateString4 = new StringBuilder(1024);
                    updateString4.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                    DynamicParameters updateStringparameters4 = new DynamicParameters();
                    updateStringparameters4.Add("@trans_code", (int)WmsEnumUtil.TransType.OUTWARD_PICK);
                    updateStringparameters4.Add("@carton_no", Stock.CartonNo);
                    updateStringparameters4.Add("@part_id", Stock.PartId);
                    updateStringparameters4.Add("@part_no", Stock.PartNo);
                    updateStringparameters4.Add("@part_spec", Stock.PartSpec);
                    updateStringparameters4.Add("@src_locate_id", Stock.LocateId);
                    updateStringparameters4.Add("@src_locate_name", Stock.LocateName);
                    updateStringparameters4.Add("@dest_locate_id", Stock.LocateId);
                    updateStringparameters4.Add("@dest_locate_name", Stock.LocateName);
                    updateStringparameters4.Add("@old_qty", Stock.Qty);
                    updateStringparameters4.Add("@new_qty", Stock.Qty);
                    updateStringparameters4.Add("@trans_qty", Stock.Qty);
                    updateStringparameters4.Add("@old_status", Stock.Status);
                    updateStringparameters4.Add("@new_status", (int)WmsEnumUtil.StockStatus.BATCHED);
                    updateStringparameters4.Add("@old_qms_status", Stock.QmsStatus);
                    updateStringparameters4.Add("@new_qms_status", Stock.QmsStatus);
                    updateStringparameters4.Add("@unit", Stock.Unit);
                    updateStringparameters4.Add("@factory_id", Stock.FactoryId);
                    updateStringparameters4.Add("@factory_code", Stock.FactoryCode);
                    updateStringparameters4.Add("@src_erp_warehouse", Stock.ErpWarehouse);
                    updateStringparameters4.Add("@dest_erp_warehouse", Stock.ErpWarehouse);
                    updateStringparameters4.Add("@src_warehouse_id", Stock.WarehouseId);
                    updateStringparameters4.Add("@src_warehouse_name", Stock.WarehouseName);
                    updateStringparameters4.Add("@dest_warehouse_id", Stock.WarehouseId);
                    updateStringparameters4.Add("@dest_warehouse_name", Stock.WarehouseName);
                    updateStringparameters4.Add("@src_zone_id", Stock.ZoneId);
                    updateStringparameters4.Add("@src_zone_name", Stock.ZoneName);
                    updateStringparameters4.Add("@dest_zone_id", Stock.ZoneId);
                    updateStringparameters4.Add("@dest_zone_name", Stock.ZoneName);
                    updateStringparameters4.Add("@enabled", Stock.Enabled);
                    updateStringparameters4.Add("@update_userid", loginId);
                    updateStringparameters4.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateStringparameters4.Add("@create_userid", loginId);
                    updateStringparameters4.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateStringparameters4.Add("@guid", Guid.NewGuid());

                    sqlStrings.Add(updateString4.ToString());
                    parameterList.Add(updateStringparameters4);
                }


                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();

                    StringBuilder stringBuilder = new StringBuilder(1024);
                    stringBuilder.Append("SELECT SUM( CONVERT(INT, c.qty)) qty,SUM(CONVERT(INT, c.pick_qty)) pick_qty,c.part_no,c.part_spec,(select top 1 locate_name from sys_stock (nolock) where part_id=c.part_id and status=" + (int)WmsEnumUtil.StockStatus.INSTOCKED + " order by create_time) as recommend_locate  FROM dbo.wms_move_header a (nolock) ");
                    stringBuilder.Append("LEFT JOIN dbo.wms_move_detail c (nolock) ON a.order_no=c.order_no ");
                    stringBuilder.Append(" WHERE a.order_no = '" + orderNo + "' AND order_status in ('30') AND order_type=" + (int)WmsEnumUtil.MoveOrderType.TRANSFER_ORDER + " and a.enabled='Y' group by c.part_id,c.part_no,c.part_spec");

                    List<WmsMoveDetail> wmsMoves1 = dbConn.Query<WmsMoveDetail>(stringBuilder.ToString()).ToList();
                    for (int i = 0; i < wmsMoves1.Count; i++)
                    {
                        if (wmsMoves1[i].Qty != wmsMoves1[i].PickQty)
                        {


                            rfInfo.type = "PASS";
                            rfInfo.message = "[" + Stock.CartonNo + "]:操作成功!请继续扫描箱条码或周转箱";
                            rfInfo.wmsObjectDetail = wmsMoves1.ConvertAll(s => (object)s);
                            return rfInfo;
                        }
                    }
                    rfInfo.type = "PASS";
                    rfInfo.message = "[" + Stock.CartonNo + "]:单据操作完成!";
                    rfInfo.wmsObjectDetail = wmsMoves1.ConvertAll(s => (object)s);
                    return rfInfo;

                }
                else
                {
                    transaction.Rollback();
                    rfInfo.type = "FAIL";
                    rfInfo.message = "未查询到数据，请检查！";
                    return rfInfo;
                }
            }
        }



        /// <summary>
        /// 获取移库接收单据信息
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        public List<WmsMoveHeader> GetMoveSourceOrderList(string orderNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("SELECT a.order_no,b.enum_desc AS order_type_desc,a.create_time,d.emp_no,a.factory_code,a.enabled  FROM dbo.wms_move_header a (nolock) ");
                SqlStringBuilder.Append("LEFT JOIN dbo.sys_enum b (nolock) ON a.order_type = b.enum_value AND b.enum_type = 'wms_move_order_type' ");
                //SqlStringBuilder.Append("LEFT JOIN wms_outstock_detail c ON a.order_no = c.order_no ");
                SqlStringBuilder.Append("LEFT JOIN dbo.sys_emp d (nolock) ON a.create_userid = d.emp_id ");
                SqlStringBuilder.Append("WHERE a.order_no like '%" + orderNo + "%' AND order_status in ('30') AND order_type = " + (int)WmsEnumUtil.MoveOrderType.TRANSFER_ORDER + " and a.enabled = 'Y' ");
                SqlStringBuilder.Append("AND order_no IN(SELECT t.order_no FROM wms_move_detail t WHERE  a.order_no= t.order_no GROUP BY t.order_no HAVING SUM(t.pick_qty)!=0) ");

                List<WmsMoveHeader> wmsMoves = dbConn.Query<WmsMoveHeader>(SqlStringBuilder.ToString()).ToList();
                return wmsMoves;

            }
        }


        /// <summary>
        /// 获取移库配料单据信息通过订单号
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        public List<WmsMoveHeader> GetMoveSourceListByOrderNo(string orderNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("SELECT a.order_no,b.enum_desc AS order_type_desc,CONVERT(INT, c.qty) qty,CONVERT(INT, c.pick_qty) pick_qty,CONVERT(INT, c.out_qty) out_qty,c.part_no ,a.create_userid,a.create_time,c.src_zone_id,c.src_zone_name,d.emp_no,a.factory_code,p.part_spec FROM dbo.wms_move_header a (nolock) ");
                SqlStringBuilder.Append("LEFT JOIN dbo.sys_enum b (nolock) ON a.order_type=b.enum_value AND	b.enum_type='wms_move_order_type'  ");
                SqlStringBuilder.Append("LEFT JOIN dbo.wms_move_detail c (nolock) ON a.order_no=c.order_no ");
                SqlStringBuilder.Append("LEFT JOIN dbo.sys_emp d (nolock) ON a.create_userid=d.emp_id ");
                SqlStringBuilder.Append("LEFT JOIN dbo.sys_part p (nolock) ON c.part_no=p.part_no ");
                SqlStringBuilder.Append("WHERE a.order_no = '" + orderNo + "' AND order_type=" + (int)WmsEnumUtil.MoveOrderType.TRANSFER_ORDER + " and a.enabled='Y' ");

                List<WmsMoveHeader> wmsMoves = dbConn.Query<WmsMoveHeader>(SqlStringBuilder.ToString()).ToList();
                return wmsMoves;
            }
        }


        public List<WmsMoveSn> GetMoveSnInfo(string orderNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                StringBuilder getqcSQL = new StringBuilder(1024);
                getqcSQL.Append(@"SELECT * FROM dbo.wms_move_sn (NOLOCK) WHERE order_no=@order_no AND enabled='Y' AND status=30 ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@order_no", orderNo);
                List<WmsMoveSn> WmsSnDetail = dbConn.Query<WmsMoveSn>(getqcSQL.ToString(), parameters).ToList();

                return WmsSnDetail;
            }
        }


        public string UpdteMoveStatus(List<WmsMoveDetail> wmsMoveDetails, string orderNo, List<WmsMoveSn> moveSn, SysStock Stock, string loginId, int locateId, string locateCode)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                SetObjectDetail rfInfo = new SetObjectDetail();//返回消息
                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                for (int i = 0; i < wmsMoveDetails.Count; i++)
                {
                    //修改移库明细表  40 已完成
                    StringBuilder updateString2 = new StringBuilder(1024);
                    updateString2.Append(@"update wms_move_detail set rec_qty=@rec_qty ,item_status=@item_status,update_userid=@update_userid,update_time=@update_time where order_no=@order_no ");
                    DynamicParameters updateStringparameters2 = new DynamicParameters();
                    updateStringparameters2.Add("@item_status", (int)WmsEnumUtil.MoveOrderDetailStatus.COMPLETED);
                    updateStringparameters2.Add("@order_no", orderNo);
                    updateStringparameters2.Add("@rec_qty", wmsMoveDetails[i].PickQty);
                    updateStringparameters2.Add("@update_userid", loginId);
                    updateStringparameters2.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    sqlStrings.Add(updateString2.ToString());
                    parameterList.Add(updateStringparameters2);
                }



                //修改条码表  50  待上架
                StringBuilder updateString7 = new StringBuilder(1024);
                updateString7.Append("update sys_stock set locate_id=@locate_id,locate_name=@locate_name,status=@status,update_userid=@update_userid,update_time=@update_time  where carton_no= @carton_no ");
                DynamicParameters updateStringparameters7 = new DynamicParameters();
                updateStringparameters7.Add("@locate_id", locateId);
                updateStringparameters7.Add("@locate_name", locateCode);
                updateStringparameters7.Add("@carton_no", Stock.CartonNo);
                updateStringparameters7.Add("@status", (int)WmsEnumUtil.StockStatus.INSTOCKED);
                updateStringparameters7.Add("@update_userid", loginId);
                updateStringparameters7.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                sqlStrings.Add(updateString7.ToString());
                parameterList.Add(updateStringparameters7);


                for (int i = 0; i < moveSn.Count; i++)
                {

                    //修改移库条码表  40 已完成
                    StringBuilder updateString3 = new StringBuilder(1024);
                    updateString3.Append(@"update wms_move_sn set status=@status,update_userid=@update_userid,update_time=@update_time where ruid=@ruid ");
                    DynamicParameters updateStringparameters3 = new DynamicParameters();
                    updateStringparameters3.Add("@status", (int)WmsEnumUtil.MoveOrderDetailStatus.COMPLETED);
                    updateStringparameters3.Add("@ruid", moveSn[i].Ruid);
                    updateStringparameters3.Add("@update_userid", loginId);
                    updateStringparameters3.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    sqlStrings.Add(updateString3.ToString());
                    parameterList.Add(updateStringparameters3);





                    //修改条码表  50  待上架
                    StringBuilder updateString5 = new StringBuilder(1024);
                    updateString5.Append("update sys_stock set locate_id=@locate_id,locate_name=@locate_name,status=@status,update_userid=@update_userid,update_time=@update_time  where carton_no= @carton_no ");
                    DynamicParameters updateStringparameters5 = new DynamicParameters();
                    updateStringparameters5.Add("@locate_id", locateId);
                    updateStringparameters5.Add("@locate_name", locateCode);
                    updateStringparameters5.Add("@carton_no", moveSn[i].CartonNo);
                    updateStringparameters5.Add("@status", (int)WmsEnumUtil.StockStatus.INSTOCKED);
                    updateStringparameters5.Add("@update_userid", loginId);
                    updateStringparameters5.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    sqlStrings.Add(updateString5.ToString());
                    parameterList.Add(updateStringparameters5);


                    //新增 sys_stock_trans 事务表
                    StringBuilder updateString4 = new StringBuilder(1024);
                    updateString4.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                    DynamicParameters updateStringparameters4 = new DynamicParameters();
                    updateStringparameters4.Add("@trans_code", (int)WmsEnumUtil.TransType.OUTWARD_PICK);
                    updateStringparameters4.Add("@carton_no", Stock.CartonNo);
                    updateStringparameters4.Add("@part_id", Stock.PartId);
                    updateStringparameters4.Add("@part_no", Stock.PartNo);
                    updateStringparameters4.Add("@part_spec", Stock.PartSpec);
                    updateStringparameters4.Add("@src_locate_id", Stock.LocateId);
                    updateStringparameters4.Add("@src_locate_name", Stock.LocateName);
                    updateStringparameters4.Add("@dest_locate_id", locateId);
                    updateStringparameters4.Add("@dest_locate_name", locateCode);
                    updateStringparameters4.Add("@old_qty", Stock.Qty);
                    updateStringparameters4.Add("@new_qty", Stock.Qty);
                    updateStringparameters4.Add("@trans_qty", Stock.Qty);
                    updateStringparameters4.Add("@old_status", Stock.Status);
                    updateStringparameters4.Add("@new_status", (int)WmsEnumUtil.StockStatus.BATCHED);
                    updateStringparameters4.Add("@old_qms_status", Stock.QmsStatus);
                    updateStringparameters4.Add("@new_qms_status", Stock.QmsStatus);
                    updateStringparameters4.Add("@unit", Stock.Unit);
                    updateStringparameters4.Add("@factory_id", Stock.FactoryId);
                    updateStringparameters4.Add("@factory_code", Stock.FactoryCode);
                    updateStringparameters4.Add("@src_erp_warehouse", Stock.ErpWarehouse);
                    updateStringparameters4.Add("@dest_erp_warehouse", Stock.ErpWarehouse);
                    updateStringparameters4.Add("@src_warehouse_id", Stock.WarehouseId);
                    updateStringparameters4.Add("@src_warehouse_name", Stock.WarehouseName);
                    updateStringparameters4.Add("@dest_warehouse_id", Stock.WarehouseId);
                    updateStringparameters4.Add("@dest_warehouse_name", Stock.WarehouseName);
                    updateStringparameters4.Add("@src_zone_id", Stock.ZoneId);
                    updateStringparameters4.Add("@src_zone_name", Stock.ZoneName);
                    updateStringparameters4.Add("@dest_zone_id", Stock.ZoneId);
                    updateStringparameters4.Add("@dest_zone_name", Stock.ZoneName);
                    updateStringparameters4.Add("@enabled", Stock.Enabled);
                    updateStringparameters4.Add("@update_userid", loginId);
                    updateStringparameters4.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateStringparameters4.Add("@create_userid", loginId);
                    updateStringparameters4.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateStringparameters4.Add("@guid", Guid.NewGuid());

                    sqlStrings.Add(updateString4.ToString());
                    parameterList.Add(updateStringparameters4);
                }


                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();
                    return "OK";
                }
                else
                {
                    return "未查询到数据，请检查！";
                }
            }
        }

        /// <summary>
        /// 根据单据号获取单据明细列表
        /// </summary>
        /// <returns></returns>
        public List<WmsMoveDetail> GetMoveDetailList(string orderNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("SELECT * FROM dbo.wms_move_detail (nolock) WHERE order_no = @orderNo AND enabled = 'Y' AND item_status='40' ");

                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@orderNo", orderNo);

                List<WmsMoveDetail> moveDetailList = dbConn.Query<WmsMoveDetail>(SqlStringBuilder.ToString(), parameters).ToList();

                return moveDetailList;
            }
        }

        //获取零件号信息
        public SysPart GetPartNOInfoByPartNo(string partNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("    SELECT * from sys_part (nolock) where part_no='" + partNo + "' ");
                SysPart sysParts = dbConn.QueryFirstOrDefault<SysPart>(SqlStringBuilder.ToString());
                return sysParts;
            }
        }


        //结果添加到接口表
        public bool InsertInterfaceByNCConcession(List<WmsMoveDetail> moveDetails)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                for (int i = 0; i < moveDetails.Count; i++)
                {
                    StringBuilder SqlStringBuilder = new StringBuilder(1024);
                    SqlStringBuilder.Append("INSERT INTO dbo.WMS_MOVR(ZINSTNO,ZEILE,WERKS,LGORT,UMLGO,MATNR,ERFMG ");
                    SqlStringBuilder.Append("           ,ERFME,ACTION,ZPOST,ZTIME, REFORDERNO ,SID,RECTIM,SYNFLG ");
                    SqlStringBuilder.Append("           ,GUID) ");
                    SqlStringBuilder.Append("     VALUES(@ZINSTNO,@ZEILE,@WERKS,@LGORT,@UMLGO,@MATNR,@ERFMG,@ERFME ");
                    SqlStringBuilder.Append("           ,@ACTION,@ZPOST,@ZTIME,@REFORDERNO,@SID,@RECTIM,@SYNFLG ");
                    SqlStringBuilder.Append("           ,@GUID) ");

                    SysPart sysPart = GetPartNOInfoByPartNo(moveDetails[i].PartNo);
                    sqlStrings.Add(SqlStringBuilder.ToString());
                    DynamicParameters parameters = new DynamicParameters();
                    parameters.Add("@ZINSTNO", moveDetails[i].OrderNo);//盘点凭证号
                    parameters.Add("@ZEILE", (i + 1) * 10);//行号
                    parameters.Add("@WERKS", moveDetails[i].FactoryCode);//工厂
                    parameters.Add("@LGORT", moveDetails[i].SrcErpWarehouse);//发出库存地点
                    parameters.Add("@UMLGO", moveDetails[i].DestErpWarehouse);//接收库存地点
                    parameters.Add("@MATNR", moveDetails[i].PartNo);//物料编码
                    parameters.Add("@ERFMG", moveDetails[i].PickQty);//转储数量
                    if (sysPart == null)
                    {
                        parameters.Add("@ERFME", "");//计量单位
                    }
                    else
                    {
                        parameters.Add("@ERFME", sysPart.Unit);//计量单位
                    }
                    parameters.Add("@ACTION", "M02"); //M02:NC让步接收
                    parameters.Add("@ZPOST", DateTime.Now.ToString("yyyyMMdd"));//交易日期
                    parameters.Add("@ZTIME", DateTime.Now.ToString("HHmmss"));//交易时间
                    parameters.Add("@REFORDERNO", moveDetails[i].RefOrderNo);//关联单号
                    parameters.Add("@SID", Guid.NewGuid().ToString().Replace("-", ""));
                    parameters.Add("@RECTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    parameters.Add("@SYNFLG", "N");
                    parameters.Add("@GUID", Guid.NewGuid().ToString());
                    parameterList.Add(parameters);
                }

                IDbTransaction transaction = dbConn.BeginTransaction();
                try
                {
                    bool successCount = true;
                    for (int i = 0; i < sqlStrings.Count; i++)
                    {
                        dbConn.Execute(sqlStrings[i], parameterList[i], transaction);
                    }
                    //提交事务
                    transaction.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    //回滚事务
                    LogHelper.Error("ExecuteTransaction", ex);
                    transaction.Rollback();
                    dbConn.Close();
                    dbConn.Dispose();
                    throw ex;
                }
                finally
                {
                    dbConn.Close();
                    dbConn.Dispose();
                }
            }
        }


        public string UpdateStockInfo(string rack, string cartonNo,int? zoneId ,string zoneName , string loginId)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;
                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                StringBuilder updateString1 = new StringBuilder(1024);
                DynamicParameters updateStringparameters1 = new DynamicParameters();
                updateString1.Append("UPDATE dbo.sys_stock SET zone_id=@zone_id ,zone_name=@zone_name ,update_userid=@update_userid ,update_time=@update_time  WHERE carton_no=@carton_no  AND enabled='Y'");
                DynamicParameters parameters = new DynamicParameters();
                updateStringparameters1.Add("@zone_id", zoneId);
                updateStringparameters1.Add("@zone_name", zoneName);
                updateStringparameters1.Add("@carton_no", rack);
                updateStringparameters1.Add("@update_userid", loginId);
                updateStringparameters1.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));

                sqlStrings.Add(updateString1.ToString());
                parameterList.Add(updateStringparameters1);



                StringBuilder updateString2 = new StringBuilder(1024);
                DynamicParameters updateStringparameters2 = new DynamicParameters();
                updateString2.Append("UPDATE dbo.sys_stock SET locate_id=0,locate_name=NULL,update_userid=@update_userid ,update_time=@update_time  WHERE carton_no=@carton_no AND enabled='Y'");
                updateStringparameters2.Add("@carton_no", cartonNo);
                updateStringparameters2.Add("@update_userid", loginId);
                updateStringparameters2.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));

                sqlStrings.Add(updateString2.ToString());
                parameterList.Add(updateStringparameters2);


                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();
                    return "OK";
                }
                else
                {
                    transaction.Rollback();
                    return "操作失败，请重新尝试！";
                }
            }
        }



        public string CheckFullBoxPicking(List<WmsRackPackage_Extend> rackPackageList, SysStock_Extend stockInfo2, string rack , string cartonNo, string loginId)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                if (rackPackageList.Count > 0)
                {
                    //StringBuilder updateString2 = new StringBuilder(1024);
                    //updateString2.Append(@"UPDATE dbo.wms_rack SET enabled='N',update_userid=@update_userid,update_time=@update_time  WHERE rack_no=@rack_no AND enabled='Y'");
                    //DynamicParameters updateStringparameters2 = new DynamicParameters();
                    //updateStringparameters2.Add("@rack_no", cartonNo);
                    //updateStringparameters2.Add("@update_userid", loginId);
                    //updateStringparameters2.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    //sqlStrings.Add(updateString2.ToString());
                    //parameterList.Add(updateStringparameters2);


                    for (int i = 0; i < rackPackageList.Count; i++)
                    {
                        StringBuilder updateString3 = new StringBuilder(1024);
                        updateString3.Append(" UPDATE dbo.wms_rack_package SET rack_no=@rackno ,update_userid=@update_userid,update_time=@update_time WHERE corton_no=@corton_no AND enabled='Y' ");
                        DynamicParameters updateStringparameters3 = new DynamicParameters();
                        updateStringparameters3.Add("@rackno", rack);
                        updateStringparameters3.Add("@corton_no", rackPackageList[i].CortonNo);
                        updateStringparameters3.Add("@update_userid", loginId);
                        updateStringparameters3.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        sqlStrings.Add(updateString3.ToString());
                        parameterList.Add(updateStringparameters3);
                    }
                }
                else 
                {
                    //新增绑定到托条码
                    DynamicParameters updateStringparameters2 = new DynamicParameters();
                    StringBuilder insterString2 = new StringBuilder(1024);
                    insterString2.Append(@"INSERT INTO [dbo].[wms_rack_package]
                                         ([rack_no]
                                         ,[corton_no]
                                         ,[packageType]
                                         ,[part_no]
                                         ,[factory_id]
                                         ,[factory_code]
                                         ,[enabled]
                                         ,[create_userid]
                                         ,[create_time]
                                         ,[update_userid]
                                         ,[update_time]
                                         ,[guid],[rack_num])
                                   VALUES (@rack_no,@corton_no,@pack_type,@part_no,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid,@rack_num)");
                    updateStringparameters2 = new DynamicParameters();
                    updateStringparameters2.Add("@rack_no", rack);
                    updateStringparameters2.Add("@corton_no", cartonNo);
                    updateStringparameters2.Add("@pack_type", 2);
                    updateStringparameters2.Add("@part_no", stockInfo2.PartNo);
                    updateStringparameters2.Add("@factory_id", stockInfo2.FactoryId);
                    updateStringparameters2.Add("@factory_code", stockInfo2.FactoryCode);
                    updateStringparameters2.Add("@enabled", "Y");
                    updateStringparameters2.Add("@create_userid", loginId);
                    updateStringparameters2.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateStringparameters2.Add("@guid", Guid.NewGuid());
                    updateStringparameters2.Add("@rack_num", stockInfo2.Qty);
                    updateStringparameters2.Add("@update_userid", null);
                    updateStringparameters2.Add("@update_time", null);

                    sqlStrings.Add(insterString2.ToString());
                    parameterList.Add(updateStringparameters2);
                }



                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();
                    return "OK";
                }
                else
                {
                    transaction.Rollback();
                    return null;
                }
            }
        }


        public List<WmsRackPackage_Extend> GetRackCartonInfo(string cortonaNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("select * from wms_rack_package (nolock) where corton_no= @corton_no and enabled='Y'");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@corton_no", cortonaNo);

                return dbConn.Query<WmsRackPackage_Extend>(sql.ToString(), parameters).ToList();
            }
        }



        public List<WmsMoveHeader> GetReturnMaterialInfo(string onLine)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder getqcSQL = new StringBuilder(1024);
                getqcSQL.Append(@"select distinct a.*,b.dest_zone_name from wms_move_header as a (nolock) left join wms_move_detail as b (nolock) on a.order_no=b.order_no  
  where a.order_status in (@CREATE,@PROCESSING) and a.order_type = '280' and a.enabled='Y' and a.wrong_sign='0' order by a.update_time   ");
                DynamicParameters parameters = new DynamicParameters();
                //parameters.Add("@order_type",(int)WmsEnumUtil.MoveOrderType.PICK ); 
                parameters.Add("@CREATE", (int)WmsEnumUtil.MoveOrderStatus.CREATE);
                parameters.Add("@PROCESSING", (int)WmsEnumUtil.MoveOrderStatus.PROCESSING);

                List<WmsMoveHeader> WmsQcDetail = dbConn.Query<WmsMoveHeader>(getqcSQL.ToString(), parameters).ToList();

                return WmsQcDetail;
            }
        }



        public SysStock_Extend GetCartonNoZoneType(string cartonNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("SELECT s.* FROM dbo.sys_stock s  LEFT JOIN dbo.sys_zone z ON s.zone_id=z.zone_id AND z.enabled='Y'  WHERE z.zone_desc LIKE '%线边%' AND s.carton_no=@carton_no  AND s.enabled='Y'");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@carton_no", cartonNo);

                return dbConn.QueryFirstOrDefault<SysStock_Extend>(sql.ToString(), parameters);
            }
        }


        public List<WmsRackPackage_Extend> GetrackPackageInfo(string rackNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("select d.src_locate_id,d.src_locate_name,p.* from wms_rack_package p  LEFT JOIN dbo.wms_move_sn s ON p.corton_no=s.carton_no AND s.enabled='Y' LEFT JOIN dbo.wms_move_detail d ON s.order_no=d.order_no AND d.enabled='Y' WHERE p.rack_no=@rack_no  AND p.enabled='Y'");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@rack_no", rackNo);

                return dbConn.Query<WmsRackPackage_Extend>(sql.ToString(), parameters).ToList();
            }
        }

        public List<WmsRackPackage_Extend> GetcartnoPackageInfo(string cortonno)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("select d.src_locate_id,d.src_locate_name,p.* from wms_rack_package p  LEFT JOIN dbo.wms_move_sn s ON p.corton_no=s.carton_no AND s.enabled='Y' LEFT JOIN dbo.wms_move_detail d ON s.order_no=d.order_no AND d.enabled='Y' WHERE p.corton_no=@corton_no  AND p.enabled='Y'");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@corton_no", cortonno);

                return dbConn.Query<WmsRackPackage_Extend>(sql.ToString(), parameters).ToList();
            }
        }


        public WmsRackPackage_Extend GetmoveHeaderInfo(string cartonNo,string orderNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("SELECT h.* FROM dbo.wms_move_header h  LEFT JOIN dbo.wms_move_sn s ON  h.order_no=s.order_no AND s.enabled='Y'  LEFT JOIN dbo.wms_move_header mh ON mh.ref_order_no=h.ref_order_no AND mh.enabled='Y'  WHERE s.carton_no=@carton_no  AND mh.order_no=@order_no AND h.enabled='Y' AND h.order_type=10 ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@carton_no", cartonNo);
                parameters.Add("@order_no", orderNo);
                return dbConn.QueryFirstOrDefault<WmsRackPackage_Extend>(sql.ToString(), parameters);
            }
        }


        public List<SysLocate> GetLocateInfo(string locate_name)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                StringBuilder LocateInfoSQL = new StringBuilder(1024);
                LocateInfoSQL.Append(@"SELECT * FROM dbo.sys_locate(NOLOCK) WHERE locate_name=@locate_name  AND enabled='Y'  ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@locate_name", locate_name);
                List<SysLocate> LocateInfo = dbConn.Query<SysLocate>(LocateInfoSQL.ToString(), parameters).ToList();

                return LocateInfo;
            }
        }

        public List<SysStock> GetLocateCapacity(string locate_id)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder GetLocateSQL = new StringBuilder(1024);
                GetLocateSQL.Append(@" select isnull(sum(qty),0) qty from sys_stock (nolock) where locate_id=@locate_id and status = @status ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@locate_id", locate_id);
                parameters.Add("@status", (int)WmsEnumUtil.StockStatus.ONLINED);
                List<SysStock> LocateInfo = dbConn.Query<SysStock>(GetLocateSQL.ToString(), parameters).ToList();

                return LocateInfo;
            }
        }


        public List<WmsMoveDetail_Extend> CheckMoveDetail(string orderNo, int status)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder getqcSQL = new StringBuilder(1024);
                getqcSQL.Append(@"select *,(select top 1 locate_name from sys_stock (nolock) where part_id=a.part_id and status=@status  and enabled='Y' and locate_name != '' and locate_name is not null order by create_time)   as recommend ,(select top 1 vendor_name from sys_stock as b left join sys_vendor as c on b.vendor_id = c.vendor_id where b.part_no = a.part_no and vendor_name is not null and vendor_name !='' ) as vendor_name  from wms_move_detail as a (nolock) where order_no=@order_no and enabled='Y'");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@order_no", orderNo);
                parameters.Add("@status", status);
                List<WmsMoveDetail_Extend> WmsMoveDetail = dbConn.Query<WmsMoveDetail_Extend>(getqcSQL.ToString(), parameters).ToList();

                return WmsMoveDetail;
            }
        }


        public SetObjectDetail UpdteStockRackNoInfo(SysStock_Extend stockInfo, string loginId, string rackNo, int locateId, string locateCode, int zoneId, string zoneCode)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                SetObjectDetail rfInfo = new SetObjectDetail();//返回消息
                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                StringBuilder updateString2 = new StringBuilder(1024);
                updateString2.Append(@"UPDATE dbo.sys_stock SET locate_id=@locate_id,locate_name=@locate_name,zone_id=@zone_id,zone_name=@zone_name,update_time=@update_time,update_userid=@update_userid WHERE carton_no=@carton_no AND enabled='Y' ");
                DynamicParameters updateStringparameters2 = new DynamicParameters();
                updateStringparameters2.Add("@locate_id", locateId);
                updateStringparameters2.Add("@locate_name", locateCode);
                updateStringparameters2.Add("@zone_id", zoneId);
                updateStringparameters2.Add("@zone_name", zoneCode);
                updateStringparameters2.Add("@carton_no", rackNo);
                updateStringparameters2.Add("@update_userid", loginId);
                updateStringparameters2.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                sqlStrings.Add(updateString2.ToString());
                parameterList.Add(updateStringparameters2);


                //新增 sys_stock_trans 事务表
                StringBuilder updateString4 = new StringBuilder(1024);
                updateString4.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                DynamicParameters updateStringparameters4 = new DynamicParameters();
                updateStringparameters4.Add("@trans_code", (int)WmsEnumUtil.TransType.OUTWARD_PICK);
                updateStringparameters4.Add("@carton_no", stockInfo.CartonNo);
                updateStringparameters4.Add("@part_id", stockInfo.PartId);
                updateStringparameters4.Add("@part_no", stockInfo.PartNo);
                updateStringparameters4.Add("@part_spec", stockInfo.PartSpec);
                updateStringparameters4.Add("@src_locate_id", stockInfo.LocateId);
                updateStringparameters4.Add("@src_locate_name", stockInfo.LocateName);
                updateStringparameters4.Add("@dest_locate_id", locateId);
                updateStringparameters4.Add("@dest_locate_name", locateCode);
                updateStringparameters4.Add("@old_qty", stockInfo.Qty);
                updateStringparameters4.Add("@new_qty", stockInfo.Qty);
                updateStringparameters4.Add("@trans_qty", stockInfo.Qty);
                updateStringparameters4.Add("@old_status", stockInfo.Status);
                updateStringparameters4.Add("@new_status", (int)WmsEnumUtil.StockStatus.BATCHED);
                updateStringparameters4.Add("@old_qms_status", stockInfo.QmsStatus);
                updateStringparameters4.Add("@new_qms_status", stockInfo.QmsStatus);
                updateStringparameters4.Add("@unit", stockInfo.Unit);
                updateStringparameters4.Add("@factory_id", stockInfo.FactoryId);
                updateStringparameters4.Add("@factory_code", stockInfo.FactoryCode);
                updateStringparameters4.Add("@src_erp_warehouse", stockInfo.ErpWarehouse);
                updateStringparameters4.Add("@dest_erp_warehouse", stockInfo.ErpWarehouse);
                updateStringparameters4.Add("@src_warehouse_id", stockInfo.WarehouseId);
                updateStringparameters4.Add("@src_warehouse_name", stockInfo.WarehouseName);
                updateStringparameters4.Add("@dest_warehouse_id", stockInfo.WarehouseId);
                updateStringparameters4.Add("@dest_warehouse_name", stockInfo.WarehouseName);
                updateStringparameters4.Add("@src_zone_id", stockInfo.ZoneId);
                updateStringparameters4.Add("@src_zone_name", stockInfo.ZoneName);
                updateStringparameters4.Add("@dest_zone_id", zoneId);
                updateStringparameters4.Add("@dest_zone_name", zoneCode);
                updateStringparameters4.Add("@enabled", stockInfo.Enabled);
                updateStringparameters4.Add("@update_userid", loginId);
                updateStringparameters4.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters4.Add("@create_userid", loginId);
                updateStringparameters4.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters4.Add("@guid", Guid.NewGuid());

                sqlStrings.Add(updateString4.ToString());
                parameterList.Add(updateStringparameters4);

                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();
                    rfInfo.type = "PASS";
                    rfInfo.message = "[" + stockInfo.CartonNo + "]:操作完成!";
                }
                return rfInfo;
            }
        }



    }
}
